<!DOCTYPE html>
<html lang="en-US" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>数据库概述 | VitePress</title>
    <meta name="description" content="A VitePress site">
    <link rel="preload stylesheet" href="/notebook/assets/style.3dbfd0c2.css" as="style">
    
    <script type="module" src="/notebook/assets/app.8aaa4cbe.js"></script>
    <link rel="preload" href="/notebook/assets/inter-roman-latin.2ed14f66.woff2" as="font" type="font/woff2" crossorigin="">
    <link rel="modulepreload" href="/notebook/assets/chunks/framework.1336c4e5.js">
    <link rel="modulepreload" href="/notebook/assets/chunks/theme.20cddc0c.js">
    <link rel="modulepreload" href="/notebook/assets/2、数据库_MySQL_MySQL核心_基础.md.d8e97f3e.lean.js">
    <script id="check-dark-light">(()=>{const e=localStorage.getItem("vitepress-theme-appearance")||"",a=window.matchMedia("(prefers-color-scheme: dark)").matches;(!e||e==="auto"?a:e==="dark")&&document.documentElement.classList.add("dark")})();</script>
  </head>
  <body>
    <div id="app"><div class="Layout" data-v-255ec12d><!--[--><!--]--><!--[--><span tabindex="-1" data-v-ae3e3f51></span><a href="#VPContent" class="VPSkipLink visually-hidden" data-v-ae3e3f51> Skip to content </a><!--]--><!----><header class="VPNav" data-v-255ec12d data-v-7e5bc4a5><div class="VPNavBar has-sidebar" data-v-7e5bc4a5 data-v-0937f67c><div class="container" data-v-0937f67c><div class="title" data-v-0937f67c><div class="VPNavBarTitle has-sidebar" data-v-0937f67c data-v-86d1bed8><a class="title" href="/notebook/" data-v-86d1bed8><!--[--><!--]--><!--[--><img class="VPImage logo" src="/notebook/Vue.png" alt data-v-8426fc1a><!--]--><!--[-->任硕的文档<!--]--><!--[--><!--]--></a></div></div><div class="content" data-v-0937f67c><div class="curtain" data-v-0937f67c></div><div class="content-body" data-v-0937f67c><!--[--><!--]--><div class="VPNavBarSearch search" style="--vp-meta-key:&#39;Meta&#39;;" data-v-0937f67c><!--[--><!----><div id="docsearch"><button type="button" class="DocSearch DocSearch-Button" aria-label="Search"><span class="DocSearch-Button-Container"><svg class="DocSearch-Search-Icon" width="20" height="20" viewBox="0 0 20 20" aria-label="search icon"><path d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" stroke="currentColor" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path></svg><span class="DocSearch-Button-Placeholder">Search</span></span><span class="DocSearch-Button-Keys"><kbd class="DocSearch-Button-Key"></kbd><kbd class="DocSearch-Button-Key">K</kbd></span></button></div><!--]--></div><nav aria-labelledby="main-nav-aria-label" class="VPNavBarMenu menu" data-v-0937f67c data-v-7f418b0f><span id="main-nav-aria-label" class="visually-hidden" data-v-7f418b0f>Main Navigation</span><!--[--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>Java学前端</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/HTML+JS.html" data-v-2f2cfafc><!--[-->HTML+JS<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/CSS.html" data-v-2f2cfafc><!--[-->CSS<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/Vue2+%E7%BB%84%E4%BB%B6.html" data-v-2f2cfafc><!--[-->Vue2+组件<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/Vue3+%E7%BB%84%E4%BB%B6.html" data-v-2f2cfafc><!--[-->Vue3+组件<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/React.html" data-v-2f2cfafc><!--[-->React<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>软件测试</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80.html" data-v-2f2cfafc><!--[-->测试基础<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E5%8E%8B%E5%8A%9B%E6%B5%8B%E8%AF%95.html" data-v-2f2cfafc><!--[-->压力测试<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>多线程</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E5%B9%B6%E5%8F%91%20&amp;%20%E5%A4%9A%E7%BA%BF%E7%A8%8B/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-2f2cfafc><!--[-->基础篇<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E5%B9%B6%E5%8F%91%20&amp;%20%E5%A4%9A%E7%BA%BF%E7%A8%8B/%E5%B9%B6%E5%8F%91%E5%AE%8C%E5%96%84.html" data-v-2f2cfafc><!--[-->进阶篇<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>开发工具</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/IDEA/Chrome.html" data-v-2f2cfafc><!--[-->Chrome<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/IDEA/IDEA%E5%9F%BA%E7%A1%80.html" data-v-2f2cfafc><!--[-->IDEA基础<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/IDEA/IDEA%E6%8F%92%E4%BB%B6.html" data-v-2f2cfafc><!--[-->IDEA插件<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/IDEA/VS%20Code.html" data-v-2f2cfafc><!--[-->VS Code<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>消息中间件</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6/RabbitMQ.html" data-v-2f2cfafc><!--[-->RabbitMQ<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6/RocketMQ.html" data-v-2f2cfafc><!--[-->RocketMQ<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6/Kafka.html" data-v-2f2cfafc><!--[-->Kafka<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6/Canal.html" data-v-2f2cfafc><!--[-->Canal<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--]--></nav><!----><div class="VPNavBarAppearance appearance" data-v-0937f67c data-v-f6a63727><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title="toggle dark mode" aria-checked="false" data-v-f6a63727 data-v-82b282f1 data-v-f3c41672><span class="check" data-v-f3c41672><span class="icon" data-v-f3c41672><!--[--><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="sun" data-v-82b282f1><path d="M12,18c-3.3,0-6-2.7-6-6s2.7-6,6-6s6,2.7,6,6S15.3,18,12,18zM12,8c-2.2,0-4,1.8-4,4c0,2.2,1.8,4,4,4c2.2,0,4-1.8,4-4C16,9.8,14.2,8,12,8z"></path><path d="M12,4c-0.6,0-1-0.4-1-1V1c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,3.6,12.6,4,12,4z"></path><path d="M12,24c-0.6,0-1-0.4-1-1v-2c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,23.6,12.6,24,12,24z"></path><path d="M5.6,6.6c-0.3,0-0.5-0.1-0.7-0.3L3.5,4.9c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C6.2,6.5,5.9,6.6,5.6,6.6z"></path><path d="M19.8,20.8c-0.3,0-0.5-0.1-0.7-0.3l-1.4-1.4c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C20.3,20.7,20,20.8,19.8,20.8z"></path><path d="M3,13H1c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S3.6,13,3,13z"></path><path d="M23,13h-2c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S23.6,13,23,13z"></path><path d="M4.2,20.8c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C4.7,20.7,4.5,20.8,4.2,20.8z"></path><path d="M18.4,6.6c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C18.9,6.5,18.6,6.6,18.4,6.6z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="moon" data-v-82b282f1><path d="M12.1,22c-0.3,0-0.6,0-0.9,0c-5.5-0.5-9.5-5.4-9-10.9c0.4-4.8,4.2-8.6,9-9c0.4,0,0.8,0.2,1,0.5c0.2,0.3,0.2,0.8-0.1,1.1c-2,2.7-1.4,6.4,1.3,8.4c2.1,1.6,5,1.6,7.1,0c0.3-0.2,0.7-0.3,1.1-0.1c0.3,0.2,0.5,0.6,0.5,1c-0.2,2.7-1.5,5.1-3.6,6.8C16.6,21.2,14.4,22,12.1,22zM9.3,4.4c-2.9,1-5,3.6-5.2,6.8c-0.4,4.4,2.8,8.3,7.2,8.7c2.1,0.2,4.2-0.4,5.8-1.8c1.1-0.9,1.9-2.1,2.4-3.4c-2.5,0.9-5.3,0.5-7.5-1.1C9.2,11.4,8.1,7.7,9.3,4.4z"></path></svg><!--]--></span></span></button></div><div class="VPSocialLinks VPNavBarSocialLinks social-links" data-v-0937f67c data-v-0394ad82 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/renshuo123/renshuo123.github.io" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>GitHub</title><path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"/></svg></a><a class="VPSocialLink no-icon" href="#" aria-label="twitter" target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Twitter</title><path d="M23.953 4.57a10 10 0 01-2.825.775 4.958 4.958 0 002.163-2.723c-.951.555-2.005.959-3.127 1.184a4.92 4.92 0 00-8.384 4.482C7.69 8.095 4.067 6.13 1.64 3.162a4.822 4.822 0 00-.666 2.475c0 1.71.87 3.213 2.188 4.096a4.904 4.904 0 01-2.228-.616v.06a4.923 4.923 0 003.946 4.827 4.996 4.996 0 01-2.212.085 4.936 4.936 0 004.604 3.417 9.867 9.867 0 01-6.102 2.105c-.39 0-.779-.023-1.17-.067a13.995 13.995 0 007.557 2.209c9.053 0 13.998-7.496 13.998-13.985 0-.21 0-.42-.015-.63A9.935 9.935 0 0024 4.59z"/></svg></a><a class="VPSocialLink no-icon" href="https://github.com/" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg t="1676028692954" class="icon" ...</path></svg></a><!--]--></div><div class="VPFlyout VPNavBarExtra extra" data-v-0937f67c data-v-40855f84 data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" aria-label="extra navigation" data-v-a7b5672a><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="icon" data-v-a7b5672a><circle cx="12" cy="12" r="2"></circle><circle cx="19" cy="12" r="2"></circle><circle cx="5" cy="12" r="2"></circle></svg></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><!----><!--[--><!--[--><!----><div class="group" data-v-40855f84><div class="item appearance" data-v-40855f84><p class="label" data-v-40855f84>Appearance</p><div class="appearance-action" data-v-40855f84><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title="toggle dark mode" aria-checked="false" data-v-40855f84 data-v-82b282f1 data-v-f3c41672><span class="check" data-v-f3c41672><span class="icon" data-v-f3c41672><!--[--><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="sun" data-v-82b282f1><path d="M12,18c-3.3,0-6-2.7-6-6s2.7-6,6-6s6,2.7,6,6S15.3,18,12,18zM12,8c-2.2,0-4,1.8-4,4c0,2.2,1.8,4,4,4c2.2,0,4-1.8,4-4C16,9.8,14.2,8,12,8z"></path><path d="M12,4c-0.6,0-1-0.4-1-1V1c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,3.6,12.6,4,12,4z"></path><path d="M12,24c-0.6,0-1-0.4-1-1v-2c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,23.6,12.6,24,12,24z"></path><path d="M5.6,6.6c-0.3,0-0.5-0.1-0.7-0.3L3.5,4.9c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C6.2,6.5,5.9,6.6,5.6,6.6z"></path><path d="M19.8,20.8c-0.3,0-0.5-0.1-0.7-0.3l-1.4-1.4c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C20.3,20.7,20,20.8,19.8,20.8z"></path><path d="M3,13H1c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S3.6,13,3,13z"></path><path d="M23,13h-2c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S23.6,13,23,13z"></path><path d="M4.2,20.8c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C4.7,20.7,4.5,20.8,4.2,20.8z"></path><path d="M18.4,6.6c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C18.9,6.5,18.6,6.6,18.4,6.6z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="moon" data-v-82b282f1><path d="M12.1,22c-0.3,0-0.6,0-0.9,0c-5.5-0.5-9.5-5.4-9-10.9c0.4-4.8,4.2-8.6,9-9c0.4,0,0.8,0.2,1,0.5c0.2,0.3,0.2,0.8-0.1,1.1c-2,2.7-1.4,6.4,1.3,8.4c2.1,1.6,5,1.6,7.1,0c0.3-0.2,0.7-0.3,1.1-0.1c0.3,0.2,0.5,0.6,0.5,1c-0.2,2.7-1.5,5.1-3.6,6.8C16.6,21.2,14.4,22,12.1,22zM9.3,4.4c-2.9,1-5,3.6-5.2,6.8c-0.4,4.4,2.8,8.3,7.2,8.7c2.1,0.2,4.2-0.4,5.8-1.8c1.1-0.9,1.9-2.1,2.4-3.4c-2.5,0.9-5.3,0.5-7.5-1.1C9.2,11.4,8.1,7.7,9.3,4.4z"></path></svg><!--]--></span></span></button></div></div></div><div class="group" data-v-40855f84><div class="item social-links" data-v-40855f84><div class="VPSocialLinks social-links-list" data-v-40855f84 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/renshuo123/renshuo123.github.io" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>GitHub</title><path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"/></svg></a><a class="VPSocialLink no-icon" href="#" aria-label="twitter" target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Twitter</title><path d="M23.953 4.57a10 10 0 01-2.825.775 4.958 4.958 0 002.163-2.723c-.951.555-2.005.959-3.127 1.184a4.92 4.92 0 00-8.384 4.482C7.69 8.095 4.067 6.13 1.64 3.162a4.822 4.822 0 00-.666 2.475c0 1.71.87 3.213 2.188 4.096a4.904 4.904 0 01-2.228-.616v.06a4.923 4.923 0 003.946 4.827 4.996 4.996 0 01-2.212.085 4.936 4.936 0 004.604 3.417 9.867 9.867 0 01-6.102 2.105c-.39 0-.779-.023-1.17-.067a13.995 13.995 0 007.557 2.209c9.053 0 13.998-7.496 13.998-13.985 0-.21 0-.42-.015-.63A9.935 9.935 0 0024 4.59z"/></svg></a><a class="VPSocialLink no-icon" href="https://github.com/" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg t="1676028692954" class="icon" ...</path></svg></a><!--]--></div></div></div><!--]--><!--]--></div></div></div><!--[--><!--]--><button type="button" class="VPNavBarHamburger hamburger" aria-label="mobile navigation" aria-expanded="false" aria-controls="VPNavScreen" data-v-0937f67c data-v-e5dd9c1c><span class="container" data-v-e5dd9c1c><span class="top" data-v-e5dd9c1c></span><span class="middle" data-v-e5dd9c1c></span><span class="bottom" data-v-e5dd9c1c></span></span></button></div></div></div></div><!----></header><div class="VPLocalNav reached-top" data-v-255ec12d data-v-5cfd5582><button class="menu" aria-expanded="false" aria-controls="VPSidebarNav" data-v-5cfd5582><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="menu-icon" data-v-5cfd5582><path d="M17,11H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h14c0.6,0,1,0.4,1,1S17.6,11,17,11z"></path><path d="M21,7H3C2.4,7,2,6.6,2,6s0.4-1,1-1h18c0.6,0,1,0.4,1,1S21.6,7,21,7z"></path><path d="M21,15H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h18c0.6,0,1,0.4,1,1S21.6,15,21,15z"></path><path d="M17,19H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h14c0.6,0,1,0.4,1,1S17.6,19,17,19z"></path></svg><span class="menu-text" data-v-5cfd5582>Menu</span></button><div class="VPLocalNavOutlineDropdown" style="--vp-vh:0px;" data-v-5cfd5582 data-v-18201f51><button data-v-18201f51>Return to top</button><!----></div></div><aside class="VPSidebar" data-v-255ec12d data-v-845b8fc6><div class="curtain" data-v-845b8fc6></div><nav class="nav" id="VPSidebarNav" aria-labelledby="sidebar-aria-label" tabindex="-1" data-v-845b8fc6><span class="visually-hidden" id="sidebar-aria-label" data-v-845b8fc6> Sidebar Navigation </span><!--[--><!--]--><!--[--><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Java</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E6%96%B0%E7%89%B9%E6%80%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java新特性</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E9%9B%86%E5%90%88.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java集合</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E9%AB%98%E7%BA%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java高级</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Linux</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/Linux%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Linux基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/Linux%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Linux新特性</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/Shell.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Shell脚本</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/%E5%AE%9E%E7%94%A8%E8%84%9A%E6%9C%AC.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>实用脚本</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/%E8%BD%AF%E4%BB%B6%E9%83%A8%E7%BD%B2.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>软件部署</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Nginx</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Nginx/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Nginx/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Nginx/%E5%AE%9E%E6%88%98%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>实战篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Nginx/%E9%9D%A2%E8%AF%95%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>面试篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>SSM</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/SSM/Maven.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Maven</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/SSM/Spring.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Spring</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/SSM/SpringMVC.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringMVC</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/SSM/SpringBatch.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringBatch</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>SpringBoot</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/3%E3%80%81SpringBoot/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/3%E3%80%81SpringBoot/%E5%BA%94%E7%94%A8%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>应用篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/3%E3%80%81SpringBoot/%E6%96%B0%E7%89%B9%E6%80%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>新特性</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/3%E3%80%81SpringBoot/%E8%BF%90%E7%BB%B4&amp;%E5%8E%9F%E7%90%86.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>运维&原理</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>SpringCloud</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringCloud</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/%E5%BF%85%E5%A4%87/Sentinel.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Sentinel</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>SpringSecurity</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/SpringSecurity/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringSecurity基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/SpringSecurity/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringSecurity进阶篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/SpringSecurity/%E9%AB%98%E7%BA%A7%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringSecurity高级篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Mybatis & MybatisPlus</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Mybatis&amp;MybatisPlus/Mybatis.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Mybatis</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Mybatis&amp;MybatisPlus/MybatisPlus.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MybatisPlus</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Mybatis&amp;MybatisPlus/JPA.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>JPA</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Git & ChatGPT</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/Git.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Git</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/Github.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Github</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/ChatGPT.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ChatGPT</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/Jenkins.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Jenkins</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/Netty.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Netty</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible has-active" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>数据库</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><section class="VPSidebarItem level-1 collapsible has-active" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>MySQL</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E4%BC%98%E5%8C%96.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL优化</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E8%AE%BE%E8%AE%A1.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL设计</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E8%BF%90%E7%BB%B4.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL运维</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>分库分表</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>Redis</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E4%BC%98%E5%8C%96.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis优化</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E5%8E%9F%E7%90%86.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis原理</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E9%AB%98%E7%BA%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis高级</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E5%AE%9E%E6%88%98.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis实战</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/%E6%9C%AC%E5%9C%B0%E7%BC%93%E5%AD%98.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>本地缓存</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>MongoDB</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MongoDB/%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MongoDB基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MongoDB/%E6%95%B4%E5%90%88.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MongoDB进阶</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>ElasticSearch</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/ElasticSearch/1%E3%80%81ES%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ES基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/ElasticSearch/3%E3%80%81ES%E9%AB%98%E7%BA%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ES高级</p><!--]--></a><!----></div><!----></div><!--]--></div></section><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/influxdb.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>InfluxDB</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Neo4j.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Neo4j</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>高并发 & 秒杀 & 分布式</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%B8%89%E9%AB%98/%E5%88%86%E5%B8%83%E5%BC%8F.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>分布式理论</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/%E5%BF%85%E5%A4%87/%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>分布式锁</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%B8%89%E9%AB%98/%E7%A7%92%E6%9D%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>秒杀</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%B8%89%E9%AB%98/%E9%AB%98%E5%8F%AF%E7%94%A8.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>高可用</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%B8%89%E9%AB%98/%E9%AB%98%E5%B9%B6%E5%8F%91.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>高并发</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>云原生</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%BA%91%E5%8E%9F%E7%94%9F/Docker.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Docker</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%BA%91%E5%8E%9F%E7%94%9F/K8S.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>K8S</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>可视化 & 监控</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E5%8F%AF%E8%A7%86%E5%8C%96%20&amp;%20%E7%9B%91%E6%8E%A7/%E7%9B%91%E6%8E%A7%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>监控基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E5%8F%AF%E8%A7%86%E5%8C%96%20&amp;%20%E7%9B%91%E6%8E%A7/%E7%9B%91%E6%8E%A7%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>监控进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E5%8F%AF%E8%A7%86%E5%8C%96%20&amp;%20%E7%9B%91%E6%8E%A7/%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%A7%E5%B1%8F.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>可视化大屏</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E5%8F%AF%E8%A7%86%E5%8C%96%20&amp;%20%E7%9B%91%E6%8E%A7/Zabbix.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Zabbix</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>学前端</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>HTML+CSS</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/1%E3%80%81HTML+CSS/HTML%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>HTML基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/1%E3%80%81HTML+CSS/CSS%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>CSS基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/1%E3%80%81HTML+CSS/%E7%BD%91%E9%A1%B5%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>网页进阶</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>JS+TS</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/JS%20%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>JS基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/JS%20%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>JS进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/ES6%20%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ES6基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/ES6%20%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ES6进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/TypeScript.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>TS基础</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>NodeJS</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/4%E3%80%81Node/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Node基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/4%E3%80%81Node/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Node进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/4%E3%80%81Node/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>项目实战</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>Vue</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/3%E3%80%81Vue/Vue3/Vue3%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Vue3进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/3%E3%80%81Vue/Vue3/Vue3%E9%AB%98%E7%BA%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Vue3高级</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/3%E3%80%81Vue/Vue3/Vue3%E6%96%B0%E8%AF%AD%E6%B3%95.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Vue3新语法</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/3%E3%80%81Vue/Vue2/Vue2%E9%A1%B9%E7%9B%AE.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>项目实战</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>小程序</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/5%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>小程序基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/5%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E4%BC%98%E5%8C%96.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>小程序优化</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/5%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F/uniapp.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>uniapp</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/5%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E9%A1%B9%E7%9B%AE.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>项目实战</p><!--]--></a><!----></div><!----></div><!--]--></div></section><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>计算机基础</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>数据结构</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>操作系统</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>设计模式</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C/%E7%BD%91%E7%BB%9C%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>计算机网络</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/UML.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>UML</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E7%AE%97%E6%B3%95/LeetCode.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>LeetCode</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>项目实战</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>云尚办公</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E4%BA%91%E5%B0%9A%E5%8A%9E%E5%85%AC/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>小兔鲜</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E5%B0%8F%E5%85%94%E9%B2%9C/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E5%B0%8F%E5%85%94%E9%B2%9C/%E8%BF%9B%E9%98%B6%E7%AF%871.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇1</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E5%B0%8F%E5%85%94%E9%B2%9C/%E8%BF%9B%E9%98%B6%E7%AF%872.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇2</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>地图</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>苍穹外卖</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E8%8B%8D%E7%A9%B9%E5%A4%96%E5%8D%96/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>黑马头条</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%BB%91%E9%A9%AC%E5%A4%B4%E6%9D%A1/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%BB%91%E9%A9%AC%E5%A4%B4%E6%9D%A1/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%BB%91%E9%A9%AC%E5%A4%B4%E6%9D%A1/%E8%BF%9B%E9%98%B6%E7%AF%872.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇2</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%BB%91%E9%A9%AC%E5%A4%B4%E6%9D%A1/%E9%AB%98%E7%BA%A7%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>高级篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E6%94%AF%E4%BB%98.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>支付</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%A1%B9%E7%9B%AE%E6%8E%A8%E8%8D%90.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>项目推荐</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0" data-v-845b8fc6 data-v-9b797284><!----><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/team.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>团队成员</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><!--]--><!--[--><!--]--></nav></aside><div class="VPContent has-sidebar" id="VPContent" data-v-255ec12d data-v-669faec9><div class="VPDoc has-sidebar has-aside" data-v-669faec9 data-v-6b87e69f><!--[--><!--]--><div class="container" data-v-6b87e69f><div class="aside" data-v-6b87e69f><div class="aside-curtain" data-v-6b87e69f></div><div class="aside-container" data-v-6b87e69f><div class="aside-content" data-v-6b87e69f><div class="VPDocAside" data-v-6b87e69f data-v-3f215769><!--[--><!--]--><!--[--><!--]--><div class="VPDocAsideOutline" data-v-3f215769 data-v-ff0f39c8><div class="content" data-v-ff0f39c8><div class="outline-marker" data-v-ff0f39c8></div><div class="outline-title" data-v-ff0f39c8>On this page</div><nav aria-labelledby="doc-outline-aria-label" data-v-ff0f39c8><span class="visually-hidden" id="doc-outline-aria-label" data-v-ff0f39c8> Table of Contents for current page </span><ul class="root" data-v-ff0f39c8 data-v-d0ee3533><!--[--><!--]--></ul></nav></div></div><!--[--><!--]--><div class="spacer" data-v-3f215769></div><!--[--><!--]--><!----><!--[--><!--]--><!--[--><!--]--></div></div></div></div><div class="content" data-v-6b87e69f><div class="content-container" data-v-6b87e69f><!--[--><!--]--><!----><main class="main" data-v-6b87e69f><div style="position:relative;" class="vp-doc _notebook_2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93_MySQL_MySQL%E6%A0%B8%E5%BF%83_%E5%9F%BA%E7%A1%80" data-v-6b87e69f><div><h1 id="数据库概述" tabindex="-1">数据库概述 <a class="header-anchor" href="#数据库概述" aria-label="Permalink to &quot;数据库概述&quot;">​</a></h1><h2 id="为什么学数据库" tabindex="-1">为什么学数据库 <a class="header-anchor" href="#为什么学数据库" aria-label="Permalink to &quot;为什么学数据库&quot;">​</a></h2><h3 id="为什么学" tabindex="-1">为什么学 <a class="header-anchor" href="#为什么学" aria-label="Permalink to &quot;为什么学&quot;">​</a></h3><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302241033415.png" alt="image-20230224103318260" style="zoom:80%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302241032843.png" alt="image-20230224103256726" style="zoom:80%;"><h3 id="学习规划⭐" tabindex="-1">学习规划⭐ <a class="header-anchor" href="#学习规划⭐" aria-label="Permalink to &quot;学习规划⭐&quot;">​</a></h3><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302241036213.png" alt="image-20230224103604116" style="zoom:80%;"><h3 id="详细规划⭐" tabindex="-1">详细规划⭐ <a class="header-anchor" href="#详细规划⭐" aria-label="Permalink to &quot;详细规划⭐&quot;">​</a></h3><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302241037364.png" alt="image-20230224103748258" style="zoom:67%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206161925079.png" alt="image-20220616192521001" style="zoom:67%;"><h2 id="数据库基础概念" tabindex="-1">数据库基础概念 <a class="header-anchor" href="#数据库基础概念" aria-label="Permalink to &quot;数据库基础概念&quot;">​</a></h2><h3 id="数据库" tabindex="-1">数据库 <a class="header-anchor" href="#数据库" aria-label="Permalink to &quot;数据库&quot;">​</a></h3><blockquote><p>Database，简称DB：按照一定的数据结构来组织、存储和管理数据的仓库</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209191040174.png" alt="image-20220919104038052" style="zoom:80%;"><h3 id="数据库管理系统" tabindex="-1">数据库管理系统 <a class="header-anchor" href="#数据库管理系统" aria-label="Permalink to &quot;数据库管理系统&quot;">​</a></h3><h4 id="dbms" tabindex="-1">DBMS <a class="header-anchor" href="#dbms" aria-label="Permalink to &quot;DBMS&quot;">​</a></h4><blockquote><p>DBMS：数据库管理系统，又称为数据库软件（产品），用于管理DB中的数据，操纵和管理数据库的大型软件</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209191041903.png" alt="image-20220919104122798" style="zoom:80%;"><h4 id="关系型数据库rdbms" tabindex="-1">关系型数据库RDBMS <a class="header-anchor" href="#关系型数据库rdbms" aria-label="Permalink to &quot;关系型数据库RDBMS&quot;">​</a></h4><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302241043242.png" alt="image-20230224104331154" style="zoom:80%;"><h4 id="非关系型数据库nosql" tabindex="-1">非关系型数据库NoSQL <a class="header-anchor" href="#非关系型数据库nosql" aria-label="Permalink to &quot;非关系型数据库NoSQL&quot;">​</a></h4><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302241044022.png" alt="image-20230224104406949" style="zoom:67%;"><h4 id="主流数据库管理系统⭐" tabindex="-1">主流数据库管理系统⭐ <a class="header-anchor" href="#主流数据库管理系统⭐" aria-label="Permalink to &quot;主流数据库管理系统⭐&quot;">​</a></h4><p>而目前主流的关系型数据库管理系统的市场占有率排名如下：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302241045899.png" alt="image-20230224104524797" style="zoom:80%;"><blockquote><p>Oracle：大型的收费数据库，Oracle公司产品，价格昂贵。</p></blockquote><blockquote><p>MySQL：开源免费的中小型数据库，后来Sun公司收购了MySQL，而Oracle又收购了Sun公司。目前Oracle推出了收费版本的MySQL，也提供了免费的社区版本。</p></blockquote><blockquote><p>SQL Server：Microsoft 公司推出的收费的中型数据库，C#、.net等语言常用</p><p>PostgreSQL：开源免费的中小型数据库</p><p>DB2：IBM公司的大型收费数据库产品</p><p>SQLLite：嵌入式的微型数据库。Android内置的数据库采用的就是该数据库。</p><p>MariaDB：开源免费的中小型数据库。是MySQL数据库的另外一个分支、衍生产品，与MySQL数据库有兼容性</p></blockquote><h3 id="sql" tabindex="-1">SQL <a class="header-anchor" href="#sql" aria-label="Permalink to &quot;SQL&quot;">​</a></h3><blockquote><p>SQL:结构化查询语言，用于和DBMS通信的语言，定义了一套操作关系型数据库统一<strong>标准</strong></p></blockquote><blockquote><p>不论我们使用的是上面的哪一个关系型数据库，最终在操作时，都是使用SQL语言来进行统一操作，因为我们前面讲到SQL语言，是操作关系型数据库的 <strong>统一标准</strong> 。所以即使我们现在学习的是MySQL，假如我们以后到了公司，使用的是别的关系型数据库，如：Oracle、DB2、SQLServer，也完全不用担心，因为操作的方式都是一致的</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209191044047.png" alt="image-20220919104445951" style="zoom:80%;"><h3 id="数据模型" tabindex="-1">数据模型 <a class="header-anchor" href="#数据模型" aria-label="Permalink to &quot;数据模型&quot;">​</a></h3><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209191051228.png" alt="image-20220919105120111" style="zoom:80%;"><h2 id="sql的语言分类⭐" tabindex="-1">SQL的语言分类⭐ <a class="header-anchor" href="#sql的语言分类⭐" aria-label="Permalink to &quot;SQL的语言分类⭐&quot;">​</a></h2><blockquote><p>SQL语句，根据其功能，主要分为四类：DDL、DML、DQL、DCL</p></blockquote><blockquote><p>DQL（Data Query Language）：数据查询语言：select</p><p>DML(Data Manipulate Language):数据操作语言：insert 、update、delete</p><p>DDL（Data Define Languge）：数据定义语言：create、drop、alter</p><p>TCL（Transaction Control Language）：事务控制语言commit、rollback</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209191054334.png" alt="image-20220919105404232" style="zoom:80%;"><h1 id="安装和启动" tabindex="-1">安装和启动 <a class="header-anchor" href="#安装和启动" aria-label="Permalink to &quot;安装和启动&quot;">​</a></h1><h2 id="windows版" tabindex="-1">Windows版 <a class="header-anchor" href="#windows版" aria-label="Permalink to &quot;Windows版&quot;">​</a></h2><h3 id="安装mysql" tabindex="-1">安装MySQL <a class="header-anchor" href="#安装mysql" aria-label="Permalink to &quot;安装MySQL&quot;">​</a></h3><blockquote><p><strong>1). 双击官方下来的安装包文件</strong></p></blockquote><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.6.17/image-20210916184347938.png" alt="image-20210916184347938"></p><blockquote><p><strong>2). 根据安装提示进行安装</strong></p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.6.17/image-20210916184532212.png" alt="image-20210916184532212" style="zoom:67%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.6.17/image-20210916184603712.png" alt="image-20210916184603712" style="zoom:74%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.6.17/image-20210916184634094.png" alt="image-20210916184634094" style="zoom:70%;"><blockquote><p>安装MySQL的相关组件，这个过程可能需要耗时几分钟，耐心等待。</p></blockquote><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.6.17/image-20210916184700031.png" alt="image-20210916184700031"></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.6.17/image-20210916184709776.png" alt="image-20210916184709776" style="zoom:96%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.6.17/image-20210916184743524.png" alt="image-20210916184743524" style="zoom:80%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.6.17/image-20210916184759021.png" alt="image-20210916184759021" style="zoom:80%;"><blockquote><p>==输入MySQL中root用户的密码,一定记得记住该密码==</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.6.17/image-20210916184814656.png" alt="image-20210916184814656" style="zoom:85%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.6.17/image-20210916184832159.png" alt="image-20210916184832159" style="zoom:85%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.6.17/image-20210916184851013.png" alt="image-20210916184851013" style="zoom:87%;"><h3 id="配置环境变量" tabindex="-1">配置环境变量 <a class="header-anchor" href="#配置环境变量" aria-label="Permalink to &quot;配置环境变量&quot;">​</a></h3><blockquote><p>安装好MySQL之后，还需要配置环境变量，这样才可以在任何目录下连接MySQL。</p></blockquote><blockquote><p>1). 在此电脑上，右键选择属性</p></blockquote><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.6.17/image-20210918161550977.png" alt="image-20210918161550977"></p><blockquote><p>2). 点击左侧的 &quot;高级系统设置&quot;，选择环境变量</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.6.17/image-20210918161646433.png" alt="image-20210918161646433" style="zoom:80%;"><blockquote><p>3). 找到 Path 系统变量, 点击 &quot;编辑&quot;</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.6.17/image-20210918161803544.png" alt="image-20210918161803544" style="zoom:80%;"><blockquote><p>4). 选择 &quot;新建&quot; , 将MySQL Server的安装目录下的bin目录添加到环境变量</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.6.17/image-20210918161954696.png" alt="image-20210918161954696" style="zoom:80%;"><h3 id="修改配置文件" tabindex="-1">修改配置文件 <a class="header-anchor" href="#修改配置文件" aria-label="Permalink to &quot;修改配置文件&quot;">​</a></h3><blockquote><p>进入服务，查看配置文件位置</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.6.17/image-20230622102824861.png" alt="image-20230622102824861" style="zoom:50%;"><blockquote><p>修改datadir，进入my.ini，datadir=&quot;D:\mysql8Data&quot;</p></blockquote><blockquote><p>在D盘创建mysql8Data，然后将C:\ProgramData\MySQL\MySQL Server 8.0\Data下的内容复制到该目录</p></blockquote><blockquote><p>必须进行复制，不然无法启动</p></blockquote><h1 id="基础命令⭐" tabindex="-1">基础命令⭐ <a class="header-anchor" href="#基础命令⭐" aria-label="Permalink to &quot;基础命令⭐&quot;">​</a></h1><h2 id="启动和停止" tabindex="-1">启动和停止 <a class="header-anchor" href="#启动和停止" aria-label="Permalink to &quot;启动和停止&quot;">​</a></h2><p>方式一：计算机——右击管理——服务，查看MySQL80服务，进行重启、暂停等操作</p><p>方式二：通过管理员身份运行</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">systemctl start mysqld</span></span></code></pre></div><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">systemctl restart mysqld</span></span></code></pre></div><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">systemctl stop mysqld</span></span></code></pre></div><h2 id="命令行登录" tabindex="-1">命令行登录 <a class="header-anchor" href="#命令行登录" aria-label="Permalink to &quot;命令行登录&quot;">​</a></h2><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mysql 【</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">h主机名 </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">P端口号 】</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">u用户名 </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p密码</span></span></code></pre></div><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mysql </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">uroot </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p123456</span></span>
<span class="line"><span style="color:#A6ACCD;">mysql </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">uroot </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p123456 </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">P </span><span style="color:#F78C6C;">3307</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">mysql </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">uroot </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p123456 </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">h </span><span style="color:#F78C6C;">192.168.0.155</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">P </span><span style="color:#F78C6C;">3307</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209081227032.png" alt="image-20220908122707748" style="zoom:80%;"><h2 id="修改默认端口号" tabindex="-1">修改默认端口号 <a class="header-anchor" href="#修改默认端口号" aria-label="Permalink to &quot;修改默认端口号&quot;">​</a></h2><blockquote><p>使用everything搜索my.ini</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302241103288.png" alt="image-20230224110304213" style="zoom:67%;"><blockquote><p>打开之后搜索port，进行修改成3306，注意有两个port，都要修改成3306，之后重启MySQL80服务即可</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302241104799.png" alt="image-20230224110435694" style="zoom:80%;"><h2 id="基本命令⭐" tabindex="-1">基本命令⭐ <a class="header-anchor" href="#基本命令⭐" aria-label="Permalink to &quot;基本命令⭐&quot;">​</a></h2><div class="language-SQL"><button title="Copy Code" class="copy"></button><span class="lang">SQL</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">.查看当前所有的数据库</span></span>
<span class="line"><span style="color:#A6ACCD;">show databases;</span></span>
<span class="line"><span style="color:#A6ACCD;"># 查询当前数据库</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">database</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">.打开指定的库</span></span>
<span class="line"><span style="color:#F78C6C;">use</span><span style="color:#A6ACCD;"> 库名</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">.查看当前库的所有表</span></span>
<span class="line"><span style="color:#A6ACCD;">show tables;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">.查看其它库的所有表</span></span>
<span class="line"><span style="color:#A6ACCD;">show tables </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> 库名;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">.创建表</span></span>
<span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">表名</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;">	列名 列类型 主键,</span></span>
<span class="line"><span style="color:#A6ACCD;">	列名 列类型，</span></span>
<span class="line"><span style="color:#A6ACCD;">	...</span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">6</span><span style="color:#A6ACCD;">.查看表结构</span></span>
<span class="line"><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;"> 表名;</span></span>
<span class="line"><span style="color:#A6ACCD;">describe 表名;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">7</span><span style="color:#A6ACCD;">.查看服务器的版本</span></span>
<span class="line"><span style="color:#A6ACCD;">#方式一：新建查询</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">version</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">#方式二：在cmd里进行查看，要配置好环境变量，不然会出错 </span></span>
<span class="line"><span style="color:#A6ACCD;">mysql </span><span style="color:#676E95;font-style:italic;">--version</span></span>
<span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">8</span><span style="color:#A6ACCD;">.查询表中所有数据</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> 表名;</span></span></code></pre></div><h3 id="查看连接的客户端详情" tabindex="-1">查看连接的客户端详情 <a class="header-anchor" href="#查看连接的客户端详情" aria-label="Permalink to &quot;查看连接的客户端详情&quot;">​</a></h3><p>每个 MySQL 命令行窗口就是一个 MySQL 客户端，每个客户端都可以单独设置（不同的）事务隔离级别，这也是演示 MySQL 并发事务的基础。以下是查询客户端连接的 SQL 命令：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">show processlist;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202206301704616.png" alt="image-20220630170442553" style="zoom:67%;"><h3 id="查询连接客户端的数量" tabindex="-1">查询连接客户端的数量 <a class="header-anchor" href="#查询连接客户端的数量" aria-label="Permalink to &quot;查询连接客户端的数量&quot;">​</a></h3><p>可以使用以下 SQL 命令，查询连当前接 MySQL 服务器的客户端数量：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">show </span><span style="color:#F78C6C;">status</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">like</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Threads%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202206301705716.png" alt="image-20220630170515658" style="zoom:67%;"><h1 id="常见数据类型" tabindex="-1">常见数据类型 <a class="header-anchor" href="#常见数据类型" aria-label="Permalink to &quot;常见数据类型&quot;">​</a></h1><blockquote><p>所选择的类型越简单越好，能保存数值的类型越小越好</p></blockquote><h2 id="数值类型" tabindex="-1">数值类型 <a class="header-anchor" href="#数值类型" aria-label="Permalink to &quot;数值类型&quot;">​</a></h2><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302241716633.png" alt="image-20230224171654542" style="zoom:80%;"><h2 id="字符串型" tabindex="-1">字符串型 <a class="header-anchor" href="#字符串型" aria-label="Permalink to &quot;字符串型&quot;">​</a></h2><blockquote><p>char(10) --------&gt;存放1个字符也会占用10个字符空间，用空格补位，性能好，性别 ： gender char(1)</p><p>varchar(10) -----&gt;存放1个字符就占用1个字符空间，性能较差，原因是要计算占用空间 ：username varchar(50)</p></blockquote><blockquote><p>MySQL 中的 <code>BLOB</code> 和 <code>TEXT</code> 字段类型可以存储数据量较大的文件，可以使用这些数据类型 存储图像、声音或者是大容量的文本内容，例如网页或者文档。虽然使用 BLOB 或者 TEXT 可 以存储大容量的数据，但是对这些字段的处理会降低数据库的性能。<strong>📢 注意：</strong> <strong>如果并非必要，可以选择只储存文件的路径</strong>。</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206161834924.png" alt="image-20220616183457829" style="zoom:80%;"><p>MySQL 是 <strong><code>不区分大小写</code></strong> 的，因此字符串比较函数也不区分大小写。</p><p>如果想执行区分大小写的比较，可以在字符串前面添加 BINARY 关键字。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 结果分别为1，0</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">DOG</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">dog</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">BINARY</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">DOG</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">dog</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h2 id="日期型" tabindex="-1">日期型 <a class="header-anchor" href="#日期型" aria-label="Permalink to &quot;日期型&quot;">​</a></h2><blockquote><p>日期赋值时，允许“不严格”语法：任何标点符都可以用做日期部分或时间部分之间的间割符。例如，&#39;98-12-31 11:30:45&#39;、&#39;98.12.31 11+30+45&#39;、&#39;98/12/31 11<em>30</em>45&#39;和&#39;98@12@31 11^30^45&#39;是等价的，对于不合法的将会转换为：0000-00-00 00:00:00</p></blockquote><h3 id="基本语法" tabindex="-1">基本语法 <a class="header-anchor" href="#基本语法" aria-label="Permalink to &quot;基本语法&quot;">​</a></h3><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206161834378.png" alt="image-20220616183434274" style="zoom:80%;"><h3 id="实战演练" tabindex="-1">实战演练 <a class="header-anchor" href="#实战演练" aria-label="Permalink to &quot;实战演练&quot;">​</a></h3><blockquote><p>创建时加入创建时间和更新时间，修改时修改更新时间，很重要的方法</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">home</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;">    id </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> auto_increment </span><span style="color:#C792EA;">primary key</span><span style="color:#A6ACCD;"> ,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">100</span><span style="color:#A6ACCD;">) ,</span></span>
<span class="line"><span style="color:#A6ACCD;">    create_time </span><span style="color:#F78C6C;">datetime</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">default</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">now</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    update_time </span><span style="color:#F78C6C;">datetime</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">default</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">now</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">now</span><span style="color:#89DDFF;">()</span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 下面进行插入数据测试</span></span>
<span class="line"><span style="color:#F78C6C;">insert into</span><span style="color:#A6ACCD;"> home(id,</span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">values</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张三</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#F78C6C;">insert into</span><span style="color:#A6ACCD;"> home(id,</span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">values</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">李四</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 更新数据，查看时间</span></span>
<span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> home </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">二百五</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">alter</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> home </span><span style="color:#F78C6C;">add</span><span style="color:#A6ACCD;"> column is_deleted </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">default</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> home </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">小六子</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302241950377.png" alt="image-20230224195026296" style="zoom:80%;"><h3 id="如何选择" tabindex="-1">如何选择 <a class="header-anchor" href="#如何选择" aria-label="Permalink to &quot;如何选择&quot;">​</a></h3><p>你会推荐使用 datetime 还是 timestamp 字段？为什么？</p><blockquote><p>正如MySQL文档描述的那样——datetime的范围是“1000-01-01 00:00:00”到“9999-12-31 23:59:59”，而timestamp的范围是 &#39;1970-01-01 00:00:01&#39; UTC 到 &#39;2038-01-09 03:14:07&#39; UTC。如果不想程序在2038年出问题，从这个方面作者建议还是使用datetime。</p></blockquote><blockquote><p>有一种观点是既不使用 DATETIME 也不使用 TIMESTAMP 字段。如果想将特定的一天作为一个整体来表示（例如生日），可以使用 DATE 类型，但是如果需要更具体的时间，不要使用 DATETIME 或 TIMESTAMP，<strong>而是使用 BIGINT，并简单地存储自纪元以来的毫秒数（如果使用的是 Java，则为 System.currentTimeMillis()）</strong></p></blockquote><p>这样有几个优点</p><blockquote><p>可以在迁移数据库时避免因为数据类型差异，比如MySQL的DATETIME类型和Oracle的DATETIME类型之间可能存在差异，timestamp类型的精度可能也存在差异，MySQL的timestamp精度不是一开始就支持毫秒精度的。</p></blockquote><blockquote><p>没有时区问题，无论是哪个时区，因为开始计算的时间不同，无论当前时间如何，跨度是一致的。</p></blockquote><blockquote><p>没有timestamp和datatime的范围问题，哪怕是datatime，8000年以后也不能用了，没准我的数据库8000年能用8000年呢。</p></blockquote><blockquote><p>需要注意的是，bigint是占用8个字节，而timestamp只占用4个字节。从MySQL 5.6.4开始，Datetime的存储空间变成了5个字节了(准确的说应该是5字节+0~3个字节的FSP分秒精度）。</p></blockquote><h3 id="timestamp问题分析" tabindex="-1">timestamp问题分析 <a class="header-anchor" href="#timestamp问题分析" aria-label="Permalink to &quot;timestamp问题分析&quot;">​</a></h3><h4 id="时区问题" tabindex="-1">时区问题 <a class="header-anchor" href="#时区问题" aria-label="Permalink to &quot;时区问题&quot;">​</a></h4><blockquote><p>TIMESTAMP支持的时间范围从1970-01-01 00:00:01.000000到2038-01-19 03:14:07.999999，使用了TIMESTAMP的应用很有可能在2038-01-19 03:14:07.999999之后宕机，同样面临这个问题的还有所有的类Unix系统，因为他们使用了time_t这一32位数字来表示时间，这就是著名的<strong>2038年问题</strong>。</p></blockquote><p>但是<code>TIMESTAMP</code>的一些设计却非常鬼畜，比如：</p><ul><li>如果表中包含<code>TIMESTAMP</code>的列，那么其建表语句有可能被系统篡改，取决于MySQL的版本和参数设置。</li><li>当<code>MySQL</code>参数<code>time_zone=system</code>时，高并发可能会引起CPU使用率暴涨，系统响应变慢甚至假死</li><li>如果存入超过范围的时间，在非严格状态下，MySql不会报错，反而会插入<code>&#39;0000-00-00 00:00:00&#39;</code></li></ul><p>虽然通过<code>TIMESTAMP</code>可以自动转换时区，代价是当<code>MySQL</code>参数<code>time_zone=system</code>时每次都会尝试获取一个全局锁，这在高并发的环境下无疑是致命的，可能会导致线程上下文频繁切换，CPU使用率暴涨，系统响应变慢甚至假死。</p><p>现在用<code>TIMESTAMP</code>比较少了，的确也应该尽量避免使用<code>TIMESTAMP</code>，MySQL在<code>TIMESTAMP</code>的设计上实在是蹩脚，如果你正在维护一个老的系统，涉及到<code>TIMESTAMP</code>的改动需要格外注意，尽量要在充分的测试后再上线。</p><h4 id="性能问题" tabindex="-1">性能问题 <a class="header-anchor" href="#性能问题" aria-label="Permalink to &quot;性能问题&quot;">​</a></h4><p>前面已经提及，<code>TIMESTAMP</code> 的上限值 2038 年很快就会到来，那时业务又将面临一次类似千年虫的问题。另外，<code>TIMESTAMP</code> 还存在潜在的性能问题。</p><blockquote><p>虽然通过<code>TIMESTAMP</code>可以自动转换时区，代价是当<code>MySQL</code>参数<code>time_zone=system</code>时每次都会尝试获取一个全局锁，这在高并发的环境下无疑是致命的，可能会导致线程上下文频繁切换，CPU 使用率暴涨，系统响应变慢甚至假死。</p></blockquote><p>虽然从毫秒数转换到类型 <code>TIMESTAMP</code> 本身需要的 CPU 指令并不多，这并不会带来直接的性能问题。但是如果使用默认的操作系统时区，则每次通过时区计算时间时，要调用操作系统底层系统函数 <code>__tz_convert()</code>，而这个函数需要额外的加锁操作，以确保这时操作系统时区没有修改。所以，当大规模并发访问时，由于热点资源竞争，产生两个问题</p><ul><li>性能不如 <code>DATETIME</code>：<code>DATETIME</code> 不存在时区转化问题。</li><li>性能抖动：海量并发时，存在性能抖动问题。</li></ul><p>为了优化<code> TIMESTAMP</code> 的使用，强烈建议你使用显式的时区，而不是操作系统时区。比如在配置文件中显示地设置时区，而不要使用系统时区：</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">mysqld</span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#A6ACCD;">time_zone </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">+08:00</span><span style="color:#89DDFF;">&quot;</span></span></code></pre></div><p>最后，通过命令 <code>mysqlslap</code> 来测试 <code>TIMESTAMP</code>、<code>DATETIME</code> 的性能，命令如下：</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 比较time_zone为System和Asia</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">Shanghai的性能对比</span></span>
<span class="line"><span style="color:#A6ACCD;">mysqlslap </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">uroot </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p123456  </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">number</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">of</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">queries</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1000000</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">concurrency</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">100</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">query</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">SELECT NOW()</span><span style="color:#89DDFF;">&#39;</span></span></code></pre></div><p>最后的性能对比如下：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202207281520471.png" alt="image-20220728152034397" style="zoom:67%;"><blockquote><p>从表中可以发现，显式指定时区的性能要远远好于直接使用操作系统时区。所以，日期字段推荐使用 <code>DATETIME</code>，没有时区转化。即便使用 <code>TIMESTAMP</code>，也需要在数据库中显式地配置时区，而不是用系统时区。</p></blockquote><h2 id="枚举类型" tabindex="-1">枚举类型 <a class="header-anchor" href="#枚举类型" aria-label="Permalink to &quot;枚举类型&quot;">​</a></h2><blockquote><p>mysql枚举的字段类型不宜插入数字，但是需求就是要用数字，怎么办？解决：mysql数据类型定义为int,枚举限定在java代码中解决。限定值的取值范围，比如性别（男，女，未知）等。</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">test4</span><span style="color:#A6ACCD;"> (  </span></span>
<span class="line"><span style="color:#A6ACCD;">  id </span><span style="color:#C792EA;">BIGINT</span><span style="color:#A6ACCD;"> UNSIGNED  </span><span style="color:#C792EA;">PRIMARY KEY</span><span style="color:#A6ACCD;"> AUTO_INCREMENT,  </span></span>
<span class="line"><span style="color:#A6ACCD;">  brand </span><span style="color:#C792EA;">VARCHAR</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">255</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">NOT NULL</span><span style="color:#A6ACCD;">,  </span></span>
<span class="line"><span style="color:#A6ACCD;">  color ENUM(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">RED</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">GREEN</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">BLUE</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">) ENGINE </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> InnoDB;</span></span></code></pre></div><p>Java代码中，枚举类，实体类正常使用属性即可</p><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">enum</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">Color</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    RED</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;">  </span></span>
<span class="line"><span style="color:#A6ACCD;">    GREEN</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;">  </span></span>
<span class="line"><span style="color:#A6ACCD;">    BLUE  </span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h1 id="ddl-库-表" tabindex="-1">DDL(库&amp;表) <a class="header-anchor" href="#ddl-库-表" aria-label="Permalink to &quot;DDL(库&amp;表)&quot;">​</a></h1><h2 id="库的操作" tabindex="-1">库的操作 <a class="header-anchor" href="#库的操作" aria-label="Permalink to &quot;库的操作&quot;">​</a></h2><blockquote><p>字符集不要用utf-8，会插不进去表情。要用utf8mb4 才是真正的UTF-8</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 使用数据库</span></span>
<span class="line"><span style="color:#F78C6C;">use</span><span style="color:#A6ACCD;"> 库名</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 创建库</span></span>
<span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">database</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">库名</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 创建库存在就不创建</span></span>
<span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">database</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">not</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">exists</span><span style="color:#A6ACCD;"> itcast;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 创建库指定字符集</span></span>
<span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">database</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">itcast</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">default</span><span style="color:#A6ACCD;"> charset utf8mb4;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 删除库</span></span>
<span class="line"><span style="color:#F78C6C;">drop</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">database</span><span style="color:#A6ACCD;"> 库名</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 删除库不存在不报错</span></span>
<span class="line"><span style="color:#F78C6C;">drop</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">database</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">exists</span><span style="color:#A6ACCD;"> 库名</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 查询所有数据库</span></span>
<span class="line"><span style="color:#A6ACCD;">show databases;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 查询当前数据库</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">database</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">；</span></span></code></pre></div><h2 id="表的操作" tabindex="-1">表的操作 <a class="header-anchor" href="#表的操作" aria-label="Permalink to &quot;表的操作&quot;">​</a></h2><h3 id="创建表⭐" tabindex="-1">创建表⭐ <a class="header-anchor" href="#创建表⭐" aria-label="Permalink to &quot;创建表⭐&quot;">​</a></h3><p>设计一张员工信息表，要求如下</p><blockquote><ol><li>编号（纯数字）</li><li>员工工号(字符串类型，长度不超过10位)</li><li>员工姓名（字符串类型，长度不超过10位）</li><li>性别（男/女，存储一个汉字）</li><li>年龄（正常人年龄，不可能存储负数）</li><li>身份证号（二代身份证号均为18位，身份证中有X这样的字符）</li><li>入职时间（取值年月日即可）</li></ol></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">not</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">exists</span><span style="color:#A6ACCD;"> emp(</span></span>
<span class="line"><span style="color:#A6ACCD;">	id </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">编号</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">	workno </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">工号</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">姓名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">	gender </span><span style="color:#C792EA;">char</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">性别</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">	age </span><span style="color:#F78C6C;">tinyint</span><span style="color:#A6ACCD;"> unsigned comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">年龄</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">	idcard </span><span style="color:#C792EA;">char</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">18</span><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">身份证号</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">	entrycard </span><span style="color:#C792EA;">date</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">入职时间</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">	</span></span>
<span class="line"><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">员工表</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h3 id="查看表结构" tabindex="-1">查看表结构 <a class="header-anchor" href="#查看表结构" aria-label="Permalink to &quot;查看表结构&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">DESC</span><span style="color:#A6ACCD;"> emp;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209191145018.png" alt="image-20220919114547912" style="zoom:80%;"><h3 id="查看建表语句" tabindex="-1">查看建表语句 <a class="header-anchor" href="#查看建表语句" aria-label="Permalink to &quot;查看建表语句&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">show </span><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> emp;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209191146639.png" alt="image-20220919114648528" style="zoom:80%;"><h3 id="修改表⭐" tabindex="-1">修改表⭐ <a class="header-anchor" href="#修改表⭐" aria-label="Permalink to &quot;修改表⭐&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> 表名 </span><span style="color:#F78C6C;">ADD</span><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">MODIFY</span><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">DROP</span><span style="color:#A6ACCD;">|CHANGE COLUMN 字段名 【字段类型】;</span></span></code></pre></div><h4 id="修改数据类型" tabindex="-1">修改数据类型 <a class="header-anchor" href="#修改数据类型" aria-label="Permalink to &quot;修改数据类型&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 将列name的长度增加到50</span></span>
<span class="line"><span style="color:#F78C6C;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">MODIFY</span><span style="color:#A6ACCD;"> COLUMN </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">VARCHAR</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">50</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h4 id="修改字段名和数据类型" tabindex="-1">修改字段名和数据类型 <a class="header-anchor" href="#修改字段名和数据类型" aria-label="Permalink to &quot;修改字段名和数据类型&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 修改字段名</span></span>
<span class="line"><span style="color:#F78C6C;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> studentinfo CHANGE  COLUMN sex gender </span><span style="color:#C792EA;">CHAR</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h4 id="修改表名" tabindex="-1">修改表名 <a class="header-anchor" href="#修改表名" aria-label="Permalink to &quot;修改表名&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 修改表名</span></span>
<span class="line"><span style="color:#F78C6C;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> stuinfo RENAME studentinfo;</span></span></code></pre></div><h4 id="修改字段类型和列级约束" tabindex="-1">修改字段类型和列级约束 <a class="header-anchor" href="#修改字段类型和列级约束" aria-label="Permalink to &quot;修改字段类型和列级约束&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 修改字段类型和列级约束</span></span>
<span class="line"><span style="color:#F78C6C;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> studentinfo </span><span style="color:#F78C6C;">MODIFY</span><span style="color:#A6ACCD;"> COLUMN borndate </span><span style="color:#C792EA;">DATE</span><span style="color:#A6ACCD;"> ;</span></span></code></pre></div><h4 id="添加字段" tabindex="-1">添加字段 <a class="header-anchor" href="#添加字段" aria-label="Permalink to &quot;添加字段&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 添加字段</span></span>
<span class="line"><span style="color:#F78C6C;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> studentinfo </span><span style="color:#F78C6C;">ADD</span><span style="color:#A6ACCD;"> COLUMN email </span><span style="color:#C792EA;">VARCHAR</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h4 id="删除字段" tabindex="-1">删除字段 <a class="header-anchor" href="#删除字段" aria-label="Permalink to &quot;删除字段&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#⑤删除字段</span></span>
<span class="line"><span style="color:#F78C6C;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> studentinfo </span><span style="color:#F78C6C;">DROP</span><span style="color:#A6ACCD;"> COLUMN email;</span></span></code></pre></div><h3 id="表的删除-清空" tabindex="-1">表的删除 &amp; 清空 <a class="header-anchor" href="#表的删除-清空" aria-label="Permalink to &quot;表的删除 &amp; 清空&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">DROP</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> [IF EXISTS] emp;</span></span>
<span class="line"><span style="color:#F78C6C;">DROP</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">IF</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">EXISTS</span><span style="color:#A6ACCD;">  emo;</span></span></code></pre></div><p>不删除表，但<strong>清空表内所有数据</strong></p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">truncate</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> 表名;</span></span></code></pre></div><h3 id="表的复制⭐" tabindex="-1">表的复制⭐ <a class="header-anchor" href="#表的复制⭐" aria-label="Permalink to &quot;表的复制⭐&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">.仅仅复制表的结构 </span><span style="color:#F78C6C;">like</span></span>
<span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">author_copy</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">LIKE</span><span style="color:#A6ACCD;"> author;</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">.复制表的结构</span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;">数据</span></span>
<span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">author_copy1</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> author;</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 复制表的结构</span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;">部分数据</span></span>
<span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">author_copy2</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> id,au_name </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> author </span><span style="color:#F78C6C;">WHERE</span><span style="color:#A6ACCD;"> id</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">101</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 仅仅复制某些字段</span></span>
<span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">author_copy3</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> id,</span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> author;</span></span></code></pre></div><h1 id="dml-增删改" tabindex="-1">DML(增删改) <a class="header-anchor" href="#dml-增删改" aria-label="Permalink to &quot;DML(增删改)&quot;">​</a></h1><h2 id="插入" tabindex="-1">插入 <a class="header-anchor" href="#插入" aria-label="Permalink to &quot;插入&quot;">​</a></h2><h3 id="基本语法-1" tabindex="-1">基本语法 <a class="header-anchor" href="#基本语法-1" aria-label="Permalink to &quot;基本语法&quot;">​</a></h3><p>1、给表中指定字段添加数据</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">insert into</span><span style="color:#A6ACCD;"> 表名(列名,...) </span><span style="color:#F78C6C;">values</span><span style="color:#A6ACCD;">(值1,...);</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 这种基本不用，限制太多</span></span>
<span class="line"><span style="color:#F78C6C;">insert into</span><span style="color:#A6ACCD;"> 表名</span></span>
<span class="line"><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> 列名</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">值,列名</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">值,...</span></span></code></pre></div><p>2、给全部字段添加数据</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">insert into</span><span style="color:#A6ACCD;"> 表名 </span><span style="color:#F78C6C;">values</span><span style="color:#A6ACCD;">(值1,值2,值3....);</span></span></code></pre></div><p>3、批量添加数据</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 方式一，带上列名，推荐</span></span>
<span class="line"><span style="color:#F78C6C;">insert into</span><span style="color:#A6ACCD;"> 表名(列名,...) </span></span>
<span class="line"><span style="color:#F78C6C;">values</span><span style="color:#A6ACCD;">(值1,...),(值1,值2,值3....);</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 方式二，不带列名</span></span>
<span class="line"><span style="color:#F78C6C;">insert into</span><span style="color:#A6ACCD;"> 表名 </span></span>
<span class="line"><span style="color:#F78C6C;">values</span><span style="color:#A6ACCD;">(值1,值2,值3....),(值1,值2,值3....);</span></span></code></pre></div><h3 id="注意事项" tabindex="-1">注意事项 <a class="header-anchor" href="#注意事项" aria-label="Permalink to &quot;注意事项&quot;">​</a></h3><blockquote><ul><li><strong>插入数据时，指定的字段类型和顺序需要和值的顺序是一一对应的</strong></li><li><strong>字符串和日期型数据应该包含在引号中</strong></li><li><strong>插入数据大小，应该在字段规定的范围内</strong></li><li><strong>字段可以为null，添加时可以直接填null</strong></li></ul></blockquote><h3 id="实战演练-1" tabindex="-1">实战演练 <a class="header-anchor" href="#实战演练-1" aria-label="Permalink to &quot;实战演练&quot;">​</a></h3><h4 id="全部字段插入" tabindex="-1">全部字段插入 <a class="header-anchor" href="#全部字段插入" aria-label="Permalink to &quot;全部字段插入&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 可以省略列名，默认所有列，而且列的顺序和表中列的顺序一致</span></span>
<span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> beauty</span></span>
<span class="line"><span style="color:#F78C6C;">VALUES</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张飞</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">男</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">NULL</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">119</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">NULL</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">NULL</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h4 id="指定字段插入数据" tabindex="-1">指定字段插入数据 <a class="header-anchor" href="#指定字段插入数据" aria-label="Permalink to &quot;指定字段插入数据&quot;">​</a></h4><p>全字段都插入值</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> beauty(id,</span><span style="color:#F78C6C;">NAME</span><span style="color:#A6ACCD;">,sex,borndate,phone,photo,boyfriend_id)</span></span>
<span class="line"><span style="color:#F78C6C;">VALUES</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">14</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">唐艺1</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">女</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1990-4-23</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1898888888</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">NULL</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><p>只插入指定字段(即可以为null的字段不写)</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> beauty(id,</span><span style="color:#F78C6C;">NAME</span><span style="color:#A6ACCD;">,sex,phone)</span></span>
<span class="line"><span style="color:#F78C6C;">VALUES</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">15</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">娜扎</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">女</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1388888888</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h4 id="使用set插入" tabindex="-1">使用set插入 <a class="header-anchor" href="#使用set插入" aria-label="Permalink to &quot;使用set插入&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> beauty</span></span>
<span class="line"><span style="color:#F78C6C;">SET</span><span style="color:#A6ACCD;"> id</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">23</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">NAME</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">刘涛</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,phone</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">999</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h4 id="批量添加数据" tabindex="-1">批量添加数据 <a class="header-anchor" href="#批量添加数据" aria-label="Permalink to &quot;批量添加数据&quot;">​</a></h4><p>当id为自增时，插入时可以设置为null，但不可以不写</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> beauty</span></span>
<span class="line"><span style="color:#F78C6C;">VALUES</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">唐艺昕1</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">女</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1990-4-23</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1898888888</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">NULL</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">唐艺昕2</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">女</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1990-4-23</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1898888888</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">NULL</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">唐艺昕3</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">女</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1990-4-23</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1898888888</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">NULL</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="拷贝插入⭐" tabindex="-1">拷贝插入⭐ <a class="header-anchor" href="#拷贝插入⭐" aria-label="Permalink to &quot;拷贝插入⭐&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 根据dept表创建dept1表</span></span>
<span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">dept1</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">like</span><span style="color:#A6ACCD;"> dept;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 把dept表中数据查询并插入到dept1中</span></span>
<span class="line"><span style="color:#F78C6C;">insert into</span><span style="color:#A6ACCD;"> dept1 </span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> dept;</span></span></code></pre></div><blockquote><p>注意事项：使用insert into tablA select * from tableB语句时，一定要确保tableB后面的where，order或者其他条件，都需要有对应的索引，来避免出现tableB全部记录被锁定的情况。</p></blockquote><h2 id="批量插入" tabindex="-1">批量插入 <a class="header-anchor" href="#批量插入" aria-label="Permalink to &quot;批量插入&quot;">​</a></h2><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">mysql</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--local-infile=1</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">-uroot</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p123456</span></span>
<span class="line"><span style="color:#FFCB6B;">use</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">itcast</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 表结构要自己创建</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 数据是CSV格式，逗号分开</span></span>
<span class="line"><span style="color:#FFCB6B;">load</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">data</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">local</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">infile</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">/root/tb_sku1.sql</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">into</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">`</span><span style="color:#FFCB6B;">tb_sku</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">fields</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">terminated</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">by</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">lines</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">terminated</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">by</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">\n</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302282100802.png" alt="image-20230228210038714" style="zoom:80%;"><h2 id="更新" tabindex="-1">更新 <a class="header-anchor" href="#更新" aria-label="Permalink to &quot;更新&quot;">​</a></h2><h3 id="基本语法-2" tabindex="-1">基本语法 <a class="header-anchor" href="#基本语法-2" aria-label="Permalink to &quot;基本语法&quot;">​</a></h3><h4 id="修改单表" tabindex="-1">修改单表 <a class="header-anchor" href="#修改单表" aria-label="Permalink to &quot;修改单表&quot;">​</a></h4><blockquote><p>如果不带where条件会修改整张表数据</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> 表名 </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> 列</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">新值,列</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">新值,... </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> 筛选条件;</span></span></code></pre></div><h4 id="修改多表" tabindex="-1">修改多表 <a class="header-anchor" href="#修改多表" aria-label="Permalink to &quot;修改多表&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- sql92语法</span></span>
<span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> 表1 别名,表2 别名</span></span>
<span class="line"><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> 列</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">值,...</span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> 连接条件</span></span>
<span class="line"><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> 筛选条件;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- sql99语法</span></span>
<span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> 表1 别名</span></span>
<span class="line"><span style="color:#A6ACCD;">inner|left|</span><span style="color:#F78C6C;">right join</span><span style="color:#A6ACCD;"> 表2 别名</span></span>
<span class="line"><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> 连接条件</span></span>
<span class="line"><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> 列</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">值,...</span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> 筛选条件;</span></span></code></pre></div><h3 id="案例" tabindex="-1">案例 <a class="header-anchor" href="#案例" aria-label="Permalink to &quot;案例&quot;">​</a></h3><h4 id="修改单表-1" tabindex="-1">修改单表 <a class="header-anchor" href="#修改单表-1" aria-label="Permalink to &quot;修改单表&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 案例1：修改beauty表中姓唐的女神的电话为13899888899</span></span>
<span class="line"><span style="color:#F78C6C;">UPDATE</span><span style="color:#A6ACCD;"> beauty </span><span style="color:#F78C6C;">SET</span><span style="color:#A6ACCD;"> phone </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">13899888899</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#F78C6C;">WHERE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">NAME</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">LIKE</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">唐%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#案例2：修改boys表中id好为2的名称为张飞，魅力值 </span><span style="color:#F78C6C;">10</span></span>
<span class="line"><span style="color:#F78C6C;">UPDATE</span><span style="color:#A6ACCD;"> boys </span><span style="color:#F78C6C;">SET</span><span style="color:#A6ACCD;"> boyname</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张飞</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,usercp</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">10</span></span>
<span class="line"><span style="color:#F78C6C;">WHERE</span><span style="color:#A6ACCD;"> id</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h4 id="修改多表-1" tabindex="-1">修改多表 <a class="header-anchor" href="#修改多表-1" aria-label="Permalink to &quot;修改多表&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#案例 </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">：修改张无忌的女朋友的手机号为114</span></span>
<span class="line"><span style="color:#F78C6C;">UPDATE</span><span style="color:#A6ACCD;"> boys bo</span></span>
<span class="line"><span style="color:#F78C6C;">INNER JOIN</span><span style="color:#A6ACCD;"> beauty b </span><span style="color:#F78C6C;">ON</span><span style="color:#A6ACCD;"> bo.</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">id</span><span style="color:#89DDFF;">`</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">b.</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">boyfriend_id</span><span style="color:#89DDFF;">`</span></span>
<span class="line"><span style="color:#F78C6C;">SET</span><span style="color:#A6ACCD;"> b.</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">phone</span><span style="color:#89DDFF;">`</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">119</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,bo.</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">userCP</span><span style="color:#89DDFF;">`</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1000</span></span>
<span class="line"><span style="color:#F78C6C;">WHERE</span><span style="color:#A6ACCD;"> bo.</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">boyName</span><span style="color:#89DDFF;">`</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张无忌</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">#案例2：修改没有男朋友的女神的男朋友编号都为2号</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F78C6C;">UPDATE</span><span style="color:#A6ACCD;"> boys bo</span></span>
<span class="line"><span style="color:#F78C6C;">RIGHT JOIN</span><span style="color:#A6ACCD;"> beauty b </span><span style="color:#F78C6C;">ON</span><span style="color:#A6ACCD;"> bo.</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">id</span><span style="color:#89DDFF;">`</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">b.</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">boyfriend_id</span><span style="color:#89DDFF;">`</span></span>
<span class="line"><span style="color:#F78C6C;">SET</span><span style="color:#A6ACCD;"> b.</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">boyfriend_id</span><span style="color:#89DDFF;">`</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">2</span></span>
<span class="line"><span style="color:#F78C6C;">WHERE</span><span style="color:#A6ACCD;"> bo.</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">id</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">IS</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">NULL</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> boys;</span></span></code></pre></div><h3 id="注意事项-1" tabindex="-1">注意事项 <a class="header-anchor" href="#注意事项-1" aria-label="Permalink to &quot;注意事项&quot;">​</a></h3><blockquote><p>不要使用update语句的影响行数做重要的业务判断！</p></blockquote><blockquote><p>业务系统中，使用update语句更新数据是再正常不过的场景，我们也经常通过update更新的行数，来做一些业务判断，类似下面的伪代码：(mybatis + mysql 场景)</p></blockquote><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">xxxMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">updateByPrimaryKeySelective</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">entity</span><span style="color:#89DDFF;">)</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span><span style="color:#89DDFF;">){</span></span>
<span class="line"><span style="color:#89DDFF;">   </span><span style="color:#676E95;font-style:italic;">//更新成功，做其它业务处理</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><blockquote><p>但是这里有一个坑，mysql中update影响行数&gt;0是有条件的，假如有一张表：里面只有一条记录（ID=1），用update更新一把ID=1的这条记录。这一行成功更新，影响行数为1，然后把这条update语句再执行一次</p></blockquote><blockquote><p>这时候，返回的影响行数为0，也就是说，当待更新的记录与原始记录旧值相同时，mysql其实并不会做任何更新。</p></blockquote><blockquote><p>换言之，如果上游传过来的数据，与数据库本身的旧值相等，没有变化时，update语句影响行数为0。这与另一种场景：&quot;<strong>更新一条并不存在的记录，影响行数返回0</strong>&quot; 无法区分。</p></blockquote><h2 id="删除" tabindex="-1">删除 <a class="header-anchor" href="#删除" aria-label="Permalink to &quot;删除&quot;">​</a></h2><h3 id="基本语法-3" tabindex="-1">基本语法 <a class="header-anchor" href="#基本语法-3" aria-label="Permalink to &quot;基本语法&quot;">​</a></h3><h4 id="单表删除" tabindex="-1">单表删除 <a class="header-anchor" href="#单表删除" aria-label="Permalink to &quot;单表删除&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">delete</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> 表名 </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> 筛选条件</span></span></code></pre></div><h4 id="多表删除" tabindex="-1">多表删除 <a class="header-anchor" href="#多表删除" aria-label="Permalink to &quot;多表删除&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- sql92语法：</span></span>
<span class="line"><span style="color:#F78C6C;">delete</span><span style="color:#A6ACCD;"> 表1的别名,表2的别名</span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> 表1 别名,表2 别名</span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> 连接条件</span></span>
<span class="line"><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> 筛选条件;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- sql99语法：</span></span>
<span class="line"><span style="color:#F78C6C;">delete</span><span style="color:#A6ACCD;"> 表1的别名,表2的别名</span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> 表1 别名</span></span>
<span class="line"><span style="color:#A6ACCD;">inner|left|</span><span style="color:#F78C6C;">right join</span><span style="color:#A6ACCD;"> 表2 别名 </span><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> 连接条件</span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> 筛选条件;</span></span></code></pre></div><h4 id="清空表" tabindex="-1">清空表 <a class="header-anchor" href="#清空表" aria-label="Permalink to &quot;清空表&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">truncate</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> 表名;</span></span></code></pre></div><h3 id="truncate、delete-与-drop" tabindex="-1">truncate、delete 与 drop <a class="header-anchor" href="#truncate、delete-与-drop" aria-label="Permalink to &quot;truncate、delete 与 drop&quot;">​</a></h3><p><strong>相同点：</strong></p><blockquote><ol><li><code>truncate</code>和不带<code>where</code>子句的<code>delete</code>、以及<code>drop</code>都会删除表内的数据。</li><li><code>drop</code>、<code>truncate</code>都是<code>DDL</code>语句（数据定义语言），执行后会自动提交。</li></ol></blockquote><p><strong>不同点</strong></p><blockquote><ol><li>truncate 和 delete 只删除数据不删除表的结构；drop 语句将删除表的结构被依赖的约束、触发器、索引；</li><li>一般来说，执行速度: drop &gt; truncate &gt; delete。</li><li>truncate删除不能回滚，delete删除可以回滚.</li><li>truncate删除没有返回值，delete删除有返回值</li><li>假如要删除的表中有自增长列，如果用delete删除后，再插入数据，自增长列的值从断点开始，而truncate删除后，再插入数据，自增长列的值从1开始</li><li>truncate删除，效率高一丢丢</li><li>delete 可以加where 条件，truncate不能加</li></ol></blockquote><h3 id="delete-limit⭐" tabindex="-1">delete+limit⭐ <a class="header-anchor" href="#delete-limit⭐" aria-label="Permalink to &quot;delete+limit⭐&quot;">​</a></h3><p>在业务场景要求高的数据库中，<code>对于单条删除和更新操作，在delete和update后面加limit 1绝对是个好习惯</code>。</p><p>比如，在删除执行中，第一条就命中了删除行，如果SQL中有limit 1；这时就return了，否则还会执行完全表扫描才return。效率不言而喻。</p><p>在日常的SQL编写中，你写delete语句时是否用到过以下SQL？</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">delete</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> t </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> sex </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">limit</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">100</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><ul><li>降低写错SQL的代价，就算删错了，比如limit 500,那也就丢了500条数据，并不致命，通过binlog也可以很快恢复数据。</li><li>避免了长事务，delete执行时MySQL会将所有涉及的行加写锁和Gap锁（间隙锁），所有DML语句执行相关行会被锁住，如果删除数量大，会直接影响相关业务无法使用。</li><li>delete数据量大时，不加limit容易把cpu打满，导致越删越慢。</li></ul><p>针对上述第二点，前提是sex上加了索引，大家都知道，加锁都是基于索引的，如果sex字段没索引，就会扫描到主键索引上，那么就算sex = 1 的只有一条记录，也会锁表。</p><h3 id="实战演练-2" tabindex="-1">实战演练 <a class="header-anchor" href="#实战演练-2" aria-label="Permalink to &quot;实战演练&quot;">​</a></h3><h4 id="单表删除-1" tabindex="-1">单表删除 <a class="header-anchor" href="#单表删除-1" aria-label="Permalink to &quot;单表删除&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#案例：删除手机号以9结尾的女神信息</span></span>
<span class="line"><span style="color:#F78C6C;">DELETE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> beauty </span><span style="color:#F78C6C;">WHERE</span><span style="color:#A6ACCD;"> phone </span><span style="color:#F78C6C;">LIKE</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%9</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h4 id="多表删除-1" tabindex="-1">多表删除 <a class="header-anchor" href="#多表删除-1" aria-label="Permalink to &quot;多表删除&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#案例：删除张无忌的女朋友的信息</span></span>
<span class="line"><span style="color:#F78C6C;">DELETE</span><span style="color:#A6ACCD;"> b</span></span>
<span class="line"><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> beauty b</span></span>
<span class="line"><span style="color:#F78C6C;">INNER JOIN</span><span style="color:#A6ACCD;"> boys bo </span><span style="color:#F78C6C;">ON</span><span style="color:#A6ACCD;"> b.</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">boyfriend_id</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> bo.</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">id</span><span style="color:#89DDFF;">`</span></span>
<span class="line"><span style="color:#F78C6C;">WHERE</span><span style="color:#A6ACCD;"> bo.</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">boyName</span><span style="color:#89DDFF;">`</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张无忌</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#案例：删除黄晓明的信息以及他女朋友的信息</span></span>
<span class="line"><span style="color:#F78C6C;">DELETE</span><span style="color:#A6ACCD;"> b,bo</span></span>
<span class="line"><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> beauty b</span></span>
<span class="line"><span style="color:#F78C6C;">INNER JOIN</span><span style="color:#A6ACCD;"> boys bo </span><span style="color:#F78C6C;">ON</span><span style="color:#A6ACCD;"> b.</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">boyfriend_id</span><span style="color:#89DDFF;">`</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">bo.</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">id</span><span style="color:#89DDFF;">`</span></span>
<span class="line"><span style="color:#F78C6C;">WHERE</span><span style="color:#A6ACCD;"> bo.</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">boyName</span><span style="color:#89DDFF;">`</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">黄晓明</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h3 id="注意事项-2" tabindex="-1">注意事项 <a class="header-anchor" href="#注意事项-2" aria-label="Permalink to &quot;注意事项&quot;">​</a></h3><blockquote><p>注意：个人建议所有的 UPDATE 和 DELETE 语句全都在 WHERE 子句中指定条件。</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> lucifer </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> age </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">22</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">xiaoliu</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p>如果省略 WHERE 子句，则 UPDATE 或 DELETE 将被应用到表中所有的行。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> lucifer </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> age </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">22</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><blockquote><p>因此，除非确实打算更新或者删除所有记录，否则要注意使用不带 WHERE 子句的 UPDATE 或 DELETE 语句。</p></blockquote><p><strong>📢 注意：</strong> 建议在对表进行更新和删除操作之前，使用 SELECT 语句确认需要删除的记录，以免造成无法挽回的结果。</p><h1 id="dcl-数据控制语言" tabindex="-1">DCL(数据控制语言) <a class="header-anchor" href="#dcl-数据控制语言" aria-label="Permalink to &quot;DCL(数据控制语言)&quot;">​</a></h1><blockquote><p>DCL英文全称是 (数据控制语言)，用来管理数据库用户 、控制数据库的访问权限</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209192233938.png" alt="image-20220919223316837" style="zoom:80%;"><h2 id="设置简单密码" tabindex="-1">设置简单密码 <a class="header-anchor" href="#设置简单密码" aria-label="Permalink to &quot;设置简单密码&quot;">​</a></h2><div class="language-mysql"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">-- MySQL8不能直接设置简单密码</span></span>
<span class="line"><span style="color:#A6ACCD;">set global validate_password.policy=0;</span></span>
<span class="line"><span style="color:#A6ACCD;">set global validate_password.length=4;</span></span></code></pre></div><p>然后正常创建用户就行</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">user</span><span style="color:#A6ACCD;"> &#39;</span><span style="color:#82AAFF;">maxwell</span><span style="color:#A6ACCD;">&#39;@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> identified </span><span style="color:#F78C6C;">by</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">123456</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h2 id="用户管理" tabindex="-1">用户管理 <a class="header-anchor" href="#用户管理" aria-label="Permalink to &quot;用户管理&quot;">​</a></h2><h3 id="_1、查询用户" tabindex="-1">1、查询用户 <a class="header-anchor" href="#_1、查询用户" aria-label="Permalink to &quot;1、查询用户&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">use</span><span style="color:#A6ACCD;"> mysql;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> user;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209192234880.png" alt="image-20220919223407783" style="zoom:80%;"><blockquote><p>其中 Host代表当前用户访问的主机, 如果为localhost, 仅代表只能够在当前本机访问，是不可以远程访问的。 User代表的是访问该数据库的用户名。在MySQL中需要通过Host和User来唯一标识一个用户。</p></blockquote><h3 id="_2、创建用户" tabindex="-1">2、创建用户 <a class="header-anchor" href="#_2、创建用户" aria-label="Permalink to &quot;2、创建用户&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">user</span><span style="color:#A6ACCD;"> &#39;</span><span style="color:#82AAFF;">用户名</span><span style="color:#A6ACCD;">&#39;@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">主机名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> identified </span><span style="color:#F78C6C;">by</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">密码</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 创建用户 itcast , 只能够在当前主机localhost访问, 密码123456;</span></span>
<span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">user</span><span style="color:#A6ACCD;"> &#39;</span><span style="color:#82AAFF;">itcast</span><span style="color:#A6ACCD;">&#39;@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">localhost</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> identified </span><span style="color:#F78C6C;">by</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">123456</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 创建用户 heima , 可以在任意主机访问该数据库, 密码123456 ;</span></span>
<span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">user</span><span style="color:#A6ACCD;"> &#39;</span><span style="color:#82AAFF;">heima</span><span style="color:#A6ACCD;">&#39;@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> identified </span><span style="color:#F78C6C;">by</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">123456</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209192235154.png" alt="image-20220919223559061" style="zoom:80%;"><h3 id="_3、修改用户密码" tabindex="-1">3、修改用户密码 <a class="header-anchor" href="#_3、修改用户密码" aria-label="Permalink to &quot;3、修改用户密码&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">alter</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">user</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">用户名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">主机名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> identified </span><span style="color:#F78C6C;">with</span><span style="color:#A6ACCD;"> mysql_native_password </span><span style="color:#F78C6C;">by</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">密码</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">alter</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">user</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">root</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">localhost</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> identified </span><span style="color:#F78C6C;">with</span><span style="color:#A6ACCD;"> mysql_native_password </span><span style="color:#F78C6C;">by</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">315217</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 修改用户 heima 的访问密码为 1234 ;</span></span>
<span class="line"><span style="color:#F78C6C;">alter</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">user</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">heima</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> identified </span><span style="color:#F78C6C;">with</span><span style="color:#A6ACCD;"> mysql_native_password </span><span style="color:#F78C6C;">by</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1234</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h3 id="_4、删除用户" tabindex="-1">4、删除用户 <a class="header-anchor" href="#_4、删除用户" aria-label="Permalink to &quot;4、删除用户&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">drop</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">user</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">用户名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">主机名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 删除itcast@localhost用户</span></span>
<span class="line"><span style="color:#F78C6C;">drop</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">user</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">itcast</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">localhost</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h2 id="权限控制" tabindex="-1">权限控制 <a class="header-anchor" href="#权限控制" aria-label="Permalink to &quot;权限控制&quot;">​</a></h2><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220120130129125.png" alt="image-20220120130129125" style="zoom:80%;"><blockquote><ul><li><strong>多个权限之间，使用逗号分隔</strong></li><li><strong>授权时，数据库名和表名可以使用星号进行通配，代表所有</strong></li></ul></blockquote><h3 id="_1、查询权限" tabindex="-1">1、查询权限 <a class="header-anchor" href="#_1、查询权限" aria-label="Permalink to &quot;1、查询权限&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">show grants for </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">用户名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">主机名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 查询权限</span></span>
<span class="line"><span style="color:#A6ACCD;">show grants for </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">root</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209192238827.png" alt="image-20220919223807734" style="zoom:80%;"><h3 id="_2、授予权限" tabindex="-1">2、授予权限 <a class="header-anchor" href="#_2、授予权限" aria-label="Permalink to &quot;2、授予权限&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">grant</span><span style="color:#A6ACCD;"> 权限列表 </span><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> 数据库名.表名 </span><span style="color:#F78C6C;">to</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">用户名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">主机名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 授予权限</span></span>
<span class="line"><span style="color:#F78C6C;">grant</span><span style="color:#A6ACCD;"> all </span><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> itcast.</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">to</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">heima</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">grant</span><span style="color:#A6ACCD;"> all </span><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">.</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">to</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">heima</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">grant</span><span style="color:#A6ACCD;"> all </span><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">.</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">to</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">root</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h3 id="_3、撤销权限" tabindex="-1">3、撤销权限 <a class="header-anchor" href="#_3、撤销权限" aria-label="Permalink to &quot;3、撤销权限&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">revoke</span><span style="color:#A6ACCD;"> 权限列表 </span><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> 数据库名.表名 </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">用户名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">主机名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 撤销权限</span></span>
<span class="line"><span style="color:#F78C6C;">revoke</span><span style="color:#A6ACCD;"> all </span><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> itcast.</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">heima</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">GRANT</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> ,REPLICATION SLAVE , REPLICATION CLIENT </span><span style="color:#F78C6C;">ON</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">.</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TO</span><span style="color:#A6ACCD;"> maxwell@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><blockquote><ul><li><strong>privileges</strong>：用户操作权限，如<code>SELECT</code>，<code>INSERT</code>，<code>UPDATE</code>、<code>DELETE</code>等，要授予所的权限则使用<code>ALL</code></li><li><strong>databasename</strong>：数据库名</li><li><strong>tablename</strong>：表名，如果要授予该用户对所有数据库和表的相应操作权限则可用<code>*</code>表示，如<code>*.*</code></li><li><strong>username</strong>：用户名</li><li><strong>host</strong>：可以访问的域名</li></ul><p>在给其他授权前，请先用管理员账户登录！</p></blockquote><h3 id="_4、刷新" tabindex="-1">4、刷新 <a class="header-anchor" href="#_4、刷新" aria-label="Permalink to &quot;4、刷新&quot;">​</a></h3><blockquote><p>更新完权限后，必须进行刷新</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">flush privileges;</span></span></code></pre></div><h2 id="实战演练⭐" tabindex="-1">实战演练⭐ <a class="header-anchor" href="#实战演练⭐" aria-label="Permalink to &quot;实战演练⭐&quot;">​</a></h2><h3 id="允许root远程登录" tabindex="-1">允许root远程登录 <a class="header-anchor" href="#允许root远程登录" aria-label="Permalink to &quot;允许root远程登录&quot;">​</a></h3><blockquote><p>默认情况下，root用户是不运行远程登录的，只允许在MySQL所在的Linux服务器登陆MySQL系统</p><p>请注意，允许root远程登录会带来安全风险</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">use</span><span style="color:#A6ACCD;"> mysql</span></span>
<span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> user </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> host</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> user</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">root</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">grant</span><span style="color:#A6ACCD;"> all privileges </span><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">.</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">to</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">root</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> User, Host </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> user;</span></span></code></pre></div><h3 id="创建用户并授予权限" tabindex="-1">创建用户并授予权限 <a class="header-anchor" href="#创建用户并授予权限" aria-label="Permalink to &quot;创建用户并授予权限&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">use</span><span style="color:#A6ACCD;"> mysql</span></span>
<span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">user</span><span style="color:#A6ACCD;"> &#39;</span><span style="color:#82AAFF;">heima</span><span style="color:#A6ACCD;">&#39;@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> identified </span><span style="color:#F78C6C;">by</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">123456</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">alter</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">user</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">heima</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> identified </span><span style="color:#F78C6C;">with</span><span style="color:#A6ACCD;"> mysql_native_password </span><span style="color:#F78C6C;">by</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1234</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">grant</span><span style="color:#A6ACCD;"> all </span><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">.</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">to</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">heima</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">flush privileges;</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> User, Host </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> user;</span></span></code></pre></div><h1 id="dql-数据查询语言-⭐⭐" tabindex="-1">DQL(数据查询语言)⭐⭐ <a class="header-anchor" href="#dql-数据查询语言-⭐⭐" aria-label="Permalink to &quot;DQL(数据查询语言)⭐⭐&quot;">​</a></h1><h2 id="基本语法⭐" tabindex="-1">基本语法⭐ <a class="header-anchor" href="#基本语法⭐" aria-label="Permalink to &quot;基本语法⭐&quot;">​</a></h2><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209191223490.png" alt="image-20220919122314347" style="zoom:80%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209192232824.png" alt="image-20220919223208727" style="zoom:80%;"><h2 id="基础查询" tabindex="-1">基础查询 <a class="header-anchor" href="#基础查询" aria-label="Permalink to &quot;基础查询&quot;">​</a></h2><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> 要查询的字段 【</span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> 表名】;</span></span></code></pre></div><blockquote><ul><li><strong>通过select查询完的结果 ，是一个虚拟的表格，不是真实存在</strong></li><li><strong>要查询的东西 可以是常量值、可以是表达式、可以是字段、可以是函数</strong></li></ul></blockquote><h3 id="简单查询" tabindex="-1">简单查询 <a class="header-anchor" href="#简单查询" aria-label="Permalink to &quot;简单查询&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">.查询表中的单个字段</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> last_name </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> employees;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">.查询表中的多个字段</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> last_name,salary,email </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> employees;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">.查询表中的所有字段</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> employees;</span></span>
<span class="line"><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">.查询表达式</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">100</span><span style="color:#A6ACCD;">%</span><span style="color:#F78C6C;">23</span><span style="color:#A6ACCD;">  </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">结果</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">.查询函数，能够查询该数据库的版本</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">version</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h3 id="起别名" tabindex="-1">起别名 <a class="header-anchor" href="#起别名" aria-label="Permalink to &quot;起别名&quot;">​</a></h3><blockquote><p>使用as | 空格 起别名，一般都用空格起别名</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#方式一：使用as</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">100</span><span style="color:#A6ACCD;">%</span><span style="color:#F78C6C;">98</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">AS</span><span style="color:#A6ACCD;"> 结果;</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> last_name </span><span style="color:#F78C6C;">AS</span><span style="color:#A6ACCD;"> 姓,first_name </span><span style="color:#F78C6C;">AS</span><span style="color:#A6ACCD;"> 名 </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> employees;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">#方式二：使用空格，一个空格即可，多个空格也行</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> last_name 姓,first_name 名 </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> employees;</span></span></code></pre></div><h3 id="去重" tabindex="-1">去重 <a class="header-anchor" href="#去重" aria-label="Permalink to &quot;去重&quot;">​</a></h3><blockquote><p>distinct，显示出表employees中的全部job_id(不能重复)，只能显示该字段</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">SELECT DISTINCT</span><span style="color:#A6ACCD;"> department_id </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> employees;</span></span></code></pre></div><h3 id="判空" tabindex="-1">判空 <a class="header-anchor" href="#判空" aria-label="Permalink to &quot;判空&quot;">​</a></h3><blockquote><ul><li><strong>避免因为查出null值导致计算出错或空指针异常之类的问题</strong></li><li>第一个参数是<strong>当前字段名</strong>，第二个参数是值为null时<strong>要进行填充的值</strong></li></ul></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> id,username,</span><span style="color:#F78C6C;">IFNULL</span><span style="color:#A6ACCD;">(create_time,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2021-09-22 15:01:50</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">) </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">create_time</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">admin</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220120133356785.png" alt="image-20220120133356785" style="zoom:80%;"><h3 id="字符拼接" tabindex="-1">字符拼接 <a class="header-anchor" href="#字符拼接" aria-label="Permalink to &quot;字符拼接&quot;">​</a></h3><blockquote><p>注意：mysql中+号不能用于字符拼接，它只能作为运算符。只能用concat</p><p>mysql中的+号：(重要)仅仅只有一个功能：运算符</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">100</span><span style="color:#89DDFF;">+</span><span style="color:#F78C6C;">90</span><span style="color:#A6ACCD;">; # 两个操作数都为数值型，则做加法运算</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">123</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">+</span><span style="color:#F78C6C;">90</span><span style="color:#A6ACCD;">; # 只要其中一方为字符型，试图将字符型数值转换成数值型，如果转换成功，则继续做加法运算</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">john</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">+</span><span style="color:#F78C6C;">90</span><span style="color:#A6ACCD;">;  # 如果转换失败，则将字符型数值转换成0</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">null</span><span style="color:#89DDFF;">+</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">; # 只要其中一方为null，则结果肯定为null</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 案例：查询员工名和姓连接成一个字段，并显示为姓名</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">concat</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">a</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">b</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">c</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">AS</span><span style="color:#A6ACCD;"> 结果;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">concat</span><span style="color:#A6ACCD;">(last_name,first_name) </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> 姓名 </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> employees;</span></span></code></pre></div><h2 id="条件查询" tabindex="-1">条件查询 <a class="header-anchor" href="#条件查询" aria-label="Permalink to &quot;条件查询&quot;">​</a></h2><h3 id="基本语法-4" tabindex="-1">基本语法 <a class="header-anchor" href="#基本语法-4" aria-label="Permalink to &quot;基本语法&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> 要查询的字段|表达式|常量值|函数  </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> 表 </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;">  条件 ;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220120135411824.png" alt="image-20220120135411824" style="zoom:80%;"><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> person </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Java</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> id</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> person </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Java</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">or</span><span style="color:#A6ACCD;"> id</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> person </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">is not null</span><span style="color:#A6ACCD;"> ;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> person </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> mobile </span><span style="color:#F78C6C;">between</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">13333333330</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">13333333334</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> person </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">in</span><span style="color:#A6ACCD;"> (</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Java</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Redis</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="实战演练-3" tabindex="-1">实战演练 <a class="header-anchor" href="#实战演练-3" aria-label="Permalink to &quot;实战演练&quot;">​</a></h3><h4 id="数据准备" tabindex="-1">数据准备 <a class="header-anchor" href="#数据准备" aria-label="Permalink to &quot;数据准备&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">emp</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;">    id  </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> auto_increment comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">ID</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">primary key</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">50</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">not null</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">姓名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    age  </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">年龄</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    job </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">职位</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    salary </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">薪资</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    entrydate </span><span style="color:#C792EA;">date</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">入职时间</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    managerid </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">直属领导ID</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    dept_id </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">部门ID</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;">)comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">员工表</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> emp (id, </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">, age, job,salary, entrydate, managerid, dept_id) </span><span style="color:#F78C6C;">VALUES</span></span>
<span class="line"><span style="color:#A6ACCD;">            (</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">金庸</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">66</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">总裁</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">20000</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2000-01-01</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">            (</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张无忌</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">项目经理</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">12500</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2005-12-05</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">            (</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">杨逍</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">33</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">开发</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">8400</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2000-11-03</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">            (</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">韦一笑</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">48</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">开发</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">11000</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2002-02-05</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">            (</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">常遇春</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">43</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">开发</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">10500</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2004-09-07</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">            (</span><span style="color:#F78C6C;">6</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">小昭</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">19</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">程序员鼓励师</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">6600</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2004-10-12</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">            (</span><span style="color:#F78C6C;">7</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">灭绝</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">60</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">财务总监</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">8500</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2002-09-12</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">            (</span><span style="color:#F78C6C;">8</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">周芷若</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">19</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">会计</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">48000</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2006-06-02</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">7</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">            (</span><span style="color:#F78C6C;">9</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">丁敏君</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">23</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">出纳</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">5250</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2009-05-13</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">7</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">            (</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">赵敏</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">市场部总监</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">12500</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2004-10-12</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">            (</span><span style="color:#F78C6C;">11</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">鹿杖客</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">56</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">职员</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">3750</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2006-10-03</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">            (</span><span style="color:#F78C6C;">12</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">鹤笔翁</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">19</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">职员</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">3750</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2007-05-09</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">            (</span><span style="color:#F78C6C;">13</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">方东白</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">19</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">职员</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">5500</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2009-02-12</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">            (</span><span style="color:#F78C6C;">14</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张三丰</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">88</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">销售总监</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">14000</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2004-10-12</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">            (</span><span style="color:#F78C6C;">15</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">俞莲舟</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">38</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">销售</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">4600</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2004-10-12</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">14</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">            (</span><span style="color:#F78C6C;">16</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">宋远桥</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">40</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">销售</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">4600</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2004-10-12</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">14</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">            (</span><span style="color:#F78C6C;">17</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">陈友谅</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">42</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2000</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2011-10-12</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h4 id="操作实战" tabindex="-1">操作实战 <a class="header-anchor" href="#操作实战" aria-label="Permalink to &quot;操作实战&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 1. 查询年龄等于 88 的员工</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> age </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">88</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 2. 查询年龄小于 20 的员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> age </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 3. 查询年龄小于等于 20 的员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> age </span><span style="color:#89DDFF;">&lt;=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 4. 查询没有身份证号的员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> idcard </span><span style="color:#F78C6C;">is</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 5. 查询有身份证号的员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> idcard </span><span style="color:#F78C6C;">is not null</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 6. 查询年龄不等于 88 的员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> age </span><span style="color:#89DDFF;">!=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">88</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> age </span><span style="color:#89DDFF;">&lt;&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">88</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 7. 查询年龄在15岁(包含) 到 20岁(包含)之间的员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> age </span><span style="color:#89DDFF;">&gt;=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">15</span><span style="color:#A6ACCD;"> &amp;&amp; age </span><span style="color:#89DDFF;">&lt;=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> age </span><span style="color:#89DDFF;">&gt;=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">15</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> age </span><span style="color:#89DDFF;">&lt;=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> age </span><span style="color:#F78C6C;">between</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">15</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 8. 查询性别为 女 且年龄小于 25 岁的员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> gender </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">女</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> age </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">25</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 9. 查询年龄等于18 或 20 或 40的员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> age </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">18</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">or</span><span style="color:#A6ACCD;"> age </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">or</span><span style="color:#A6ACCD;"> age </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">40</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> age </span><span style="color:#F78C6C;">in</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">18</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">40</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 10. 查询姓名为两个字的员工信息 _ %</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">like</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">__</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 11. 查询身份证号最后一位是X的员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> idcard </span><span style="color:#F78C6C;">like</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%X</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> idcard </span><span style="color:#F78C6C;">like</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">________________%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h3 id="算数运算符" tabindex="-1">算数运算符 <a class="header-anchor" href="#算数运算符" aria-label="Permalink to &quot;算数运算符&quot;">​</a></h3><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202207120950812.png" alt="image-20220712095056728" style="zoom:80%;"><h3 id="逻辑表达式" tabindex="-1">逻辑表达式 <a class="header-anchor" href="#逻辑表达式" aria-label="Permalink to &quot;逻辑表达式&quot;">​</a></h3><p>逻辑运算符用来判断表达式的真假。如果表达式是真，结果返回 1。如果表达式是假，结果返回 0</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202207120951809.png" alt="image-20220712095146736" style="zoom:67%;"><p>示例：salary&gt;10000 &amp;&amp; salary&lt;20000</p><blockquote><ul><li>and（&amp;&amp;）:两个条件如果同时成立，结果为true，否则为false</li><li>or(||)：两个条件只要有一个成立，结果为true，否则为false</li><li>not(!)：如果条件成立，则not后为false，否则为true</li></ul></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#案例1：查询工资z在10000到20000之间的员工名、工资</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> last_name,salary</span></span>
<span class="line"><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> employees</span></span>
<span class="line"><span style="color:#F78C6C;">WHERE</span><span style="color:#A6ACCD;"> salary</span><span style="color:#89DDFF;">&gt;=</span><span style="color:#F78C6C;">10000</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">AND</span><span style="color:#A6ACCD;"> salary</span><span style="color:#89DDFF;">&lt;=</span><span style="color:#F78C6C;">20000</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">#案例2：查询部门编号不是在90到110之间，或者工资高于15000的员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> employees</span></span>
<span class="line"><span style="color:#F78C6C;">WHERE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">NOT</span><span style="color:#A6ACCD;">(department_id</span><span style="color:#89DDFF;">&gt;=</span><span style="color:#F78C6C;">90</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">AND</span><span style="color:#A6ACCD;">  department_id</span><span style="color:#89DDFF;">&lt;=</span><span style="color:#F78C6C;">110</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">OR</span><span style="color:#A6ACCD;"> salary</span><span style="color:#89DDFF;">&gt;</span><span style="color:#F78C6C;">15000</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h2 id="聚合函数" tabindex="-1">聚合函数 <a class="header-anchor" href="#聚合函数" aria-label="Permalink to &quot;聚合函数&quot;">​</a></h2><blockquote><p>注意：聚合函数不统计NULL值</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209191246576.png" alt="image-20220919124613440" style="zoom:80%;"><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">count</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">avg</span><span style="color:#A6ACCD;">(age) </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">max</span><span style="color:#A6ACCD;">(age) </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">min</span><span style="color:#A6ACCD;">(age) </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">sum</span><span style="color:#A6ACCD;">(age) </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">count</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">),</span><span style="color:#82AAFF;">avg</span><span style="color:#A6ACCD;">(age),</span><span style="color:#82AAFF;">max</span><span style="color:#A6ACCD;">(age),</span><span style="color:#82AAFF;">min</span><span style="color:#A6ACCD;">(age),</span><span style="color:#82AAFF;">sum</span><span style="color:#A6ACCD;">(age) </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209191250427.png" alt="image-20220919125000299" style="zoom:80%;"><h2 id="分组查询⭐" tabindex="-1">分组查询⭐ <a class="header-anchor" href="#分组查询⭐" aria-label="Permalink to &quot;分组查询⭐&quot;">​</a></h2><h3 id="基本语法-5" tabindex="-1">基本语法 <a class="header-anchor" href="#基本语法-5" aria-label="Permalink to &quot;基本语法&quot;">​</a></h3><blockquote><p>注意：<strong>分组查询查询的字段一般是聚合函数和分组字段，查询其他字段是没有意义的</strong></p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> 查询的字段，分组函数</span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> 表</span></span>
<span class="line"><span style="color:#A6ACCD;">【</span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> 筛选条件】</span></span>
<span class="line"><span style="color:#F78C6C;">group by</span><span style="color:#A6ACCD;"> 分组的字段</span></span>
<span class="line"><span style="color:#A6ACCD;">【</span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> 排序的字段】</span></span>
<span class="line"><span style="color:#A6ACCD;">【</span><span style="color:#F78C6C;">having</span><span style="color:#A6ACCD;"> 分组后的过滤条件】;</span></span></code></pre></div><h3 id="实战演练-4" tabindex="-1">实战演练 <a class="header-anchor" href="#实战演练-4" aria-label="Permalink to &quot;实战演练&quot;">​</a></h3><div class="language-sqlite"><button title="Copy Code" class="copy"></button><span class="lang">sqlite</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">-- 根据职位分组，统计不同职位员工的数量</span></span>
<span class="line"><span style="color:#A6ACCD;">select job,count(*) from emp group by job;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 根据职位分组，统计不同职位员工的平均薪资</span></span>
<span class="line"><span style="color:#A6ACCD;">select job,avg(salary) &#39;平均薪资&#39; from emp group by job;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 查询年龄小于45的员工，并根据职称分组，获取员工数量大于等于3的职位</span></span>
<span class="line"><span style="color:#A6ACCD;">select job,count(*) job_count from emp where age &lt; 45 group by job having job_count &gt;=2;</span></span></code></pre></div><h2 id="排序查询" tabindex="-1">排序查询 <a class="header-anchor" href="#排序查询" aria-label="Permalink to &quot;排序查询&quot;">​</a></h2><h3 id="基本语法-6" tabindex="-1">基本语法 <a class="header-anchor" href="#基本语法-6" aria-label="Permalink to &quot;基本语法&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> 查询字段 </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> 表 </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> 条件</span></span>
<span class="line"><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> 排序的字段|表达式|函数|别名 【</span><span style="color:#F78C6C;">asc</span><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;">】</span></span></code></pre></div><blockquote><ul><li><strong>asc代表的是升序，可以省略 , desc代表的是降序</strong></li><li><strong>order by子句可以支持 单个字段、别名、表达式、函数、多个字段</strong></li><li><strong>order by子句在查询语句的最后面，除了limit子句</strong></li></ul></blockquote><h3 id="实战演练-5" tabindex="-1">实战演练 <a class="header-anchor" href="#实战演练-5" aria-label="Permalink to &quot;实战演练&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 1. 根据年龄对公司的员工进行升序排序</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> age </span><span style="color:#F78C6C;">asc</span><span style="color:#A6ACCD;"> ;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> age </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;"> ;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> age;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 2. 根据入职时间，对员工进行降序排序</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> entrydate </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;"> ;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 3. 根据年龄对公司的员工进行升序排序，年龄相同，再按照入职时间进行降序排序</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> age </span><span style="color:#F78C6C;">asc</span><span style="color:#A6ACCD;"> , entrydate </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;"> ;</span></span></code></pre></div><h4 id="添加筛选条件再排序" tabindex="-1">添加筛选条件再排序 <a class="header-anchor" href="#添加筛选条件再排序" aria-label="Permalink to &quot;添加筛选条件再排序&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#案例：查询部门编号</span><span style="color:#89DDFF;">&gt;=</span><span style="color:#A6ACCD;">90的员工信息，并按员工编号降序</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span></span>
<span class="line"><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> employees</span></span>
<span class="line"><span style="color:#F78C6C;">WHERE</span><span style="color:#A6ACCD;"> department_id</span><span style="color:#89DDFF;">&gt;=</span><span style="color:#F78C6C;">90</span></span>
<span class="line"><span style="color:#F78C6C;">ORDER BY</span><span style="color:#A6ACCD;"> employee_id </span><span style="color:#F78C6C;">DESC</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h4 id="按表达式排序" tabindex="-1">按表达式排序 <a class="header-anchor" href="#按表达式排序" aria-label="Permalink to &quot;按表达式排序&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#案例：查询员工信息 按年薪降序</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">,salary</span><span style="color:#89DDFF;">*</span><span style="color:#F78C6C;">12</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">+</span><span style="color:#F78C6C;">IFNULL</span><span style="color:#A6ACCD;">(commission_pct,</span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">))</span></span>
<span class="line"><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> employees</span></span>
<span class="line"><span style="color:#F78C6C;">ORDER BY</span><span style="color:#A6ACCD;"> salary</span><span style="color:#89DDFF;">*</span><span style="color:#F78C6C;">12</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">+</span><span style="color:#F78C6C;">IFNULL</span><span style="color:#A6ACCD;">(commission_pct,</span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">)) </span><span style="color:#F78C6C;">DESC</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h4 id="按别名排序" tabindex="-1">按别名排序 <a class="header-anchor" href="#按别名排序" aria-label="Permalink to &quot;按别名排序&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#案例：查询员工信息 按年薪升序</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">,salary</span><span style="color:#89DDFF;">*</span><span style="color:#F78C6C;">12</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">+</span><span style="color:#F78C6C;">IFNULL</span><span style="color:#A6ACCD;">(commission_pct,</span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">)) 年薪</span></span>
<span class="line"><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> employees</span></span>
<span class="line"><span style="color:#F78C6C;">ORDER BY</span><span style="color:#A6ACCD;"> 年薪 </span><span style="color:#F78C6C;">ASC</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h4 id="按函数排序" tabindex="-1">按函数排序 <a class="header-anchor" href="#按函数排序" aria-label="Permalink to &quot;按函数排序&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#案例：查询员工名，并且按名字的长度降序</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">LENGTH</span><span style="color:#A6ACCD;">(last_name),last_name</span></span>
<span class="line"><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> employees</span></span>
<span class="line"><span style="color:#F78C6C;">ORDER BY</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">LENGTH</span><span style="color:#A6ACCD;">(last_name) </span><span style="color:#F78C6C;">DESC</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h4 id="按多个字段排序" tabindex="-1">按多个字段排序 <a class="header-anchor" href="#按多个字段排序" aria-label="Permalink to &quot;按多个字段排序&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#案例：查询员工信息，要求先按工资降序，再按employee_id升序</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> employees</span></span>
<span class="line"><span style="color:#F78C6C;">ORDER BY</span><span style="color:#A6ACCD;"> salary </span><span style="color:#F78C6C;">DESC</span><span style="color:#A6ACCD;">,employee_id </span><span style="color:#F78C6C;">ASC</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h3 id="排序特性" tabindex="-1">排序特性 <a class="header-anchor" href="#排序特性" aria-label="Permalink to &quot;排序特性&quot;">​</a></h3><h4 id="问题场景" tabindex="-1">问题场景 <a class="header-anchor" href="#问题场景" aria-label="Permalink to &quot;问题场景&quot;">​</a></h4><p>新上线一个交易记录导出功能，逻辑很简单：根据查询条件，导出对应的数据。由于数据量比较大，在查询数据库时采用了分页查询，每次查询1000条数据。</p><p>自测正常，测试环境正常，上线之后运营反馈导出的<strong>数据有重复记录</strong>。</p><p>原本是以为业务逻辑问题，重新Review了一遍代码，依旧未找到问题原因。最后只好把SQL语句拿出来单独执行，导出数据，对比发现竟然是SQL语句查询结果乱序导致的。</p><h4 id="原因分析" tabindex="-1">原因分析 <a class="header-anchor" href="#原因分析" aria-label="Permalink to &quot;原因分析&quot;">​</a></h4><p>查询语句以create_time进行倒序排序，通过limit进行分页，在正常情况下不会出现问题。但当业务并发量比较大，导致create_time存在大量相同值时，再基于limit进行分页，就会出现乱序问题。</p><p>出现的场景是：<strong>以create_time排序，当create_time存在相同值，通过limit分页，导致分页数据乱序</strong>。</p><p>比如，查询1000条数据，其中有一批create_time记录值都为”2021-10-28 12:12:12“，当创建时间相同的这些数据，一部分出现在第一页，一部分出现在第二页，在查询第二页的数据时，可能会出现第一页已经查过的数据。</p><p>也就是说，数据会来回跳动，一会儿出现在第一页，一会儿出现在第二页，这就导致的数据一部分重复，一部分缺失。</p><blockquote><p>简单来说就是：<code>ORDER BY查询的数据，如果ORDER BY列存在多行相同数据，MySQL会随机返回</code>。这就会导致虽然使用了排序，但也会发生乱序的状况。</p></blockquote><h4 id="解决方案" tabindex="-1">解决方案 <a class="header-anchor" href="#解决方案" aria-label="Permalink to &quot;解决方案&quot;">​</a></h4><p>针对上述问题，基本的解决思路是：<strong>避免ORDER BY列的值出现重复</strong>。因此，可以加入ID等其他排序列</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> tb_order </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> create_time ,id </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p>这样，在create_time相同时，会根据id进行排序，而id肯定是不同的，就再不会出现上述问题了。</p><h2 id="案例巩固⭐" tabindex="-1">案例巩固⭐ <a class="header-anchor" href="#案例巩固⭐" aria-label="Permalink to &quot;案例巩固⭐&quot;">​</a></h2><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302261551634.png" alt="image-20230226155121532" style="zoom:80%;"><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 1、查询年龄为20，21，22，24岁的女性员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> gender </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">女</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> age </span><span style="color:#F78C6C;">in</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">21</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">22</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">24</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 2、查询性别为男，并且年龄在20-40岁(含)以内的姓名为三个字的员工</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> gender </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">男</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> (age </span><span style="color:#F78C6C;">between</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">40</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">like</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">___</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 3、统计员工表中，年龄小于60岁的，男性员工和女性员工的人数</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> gender,</span><span style="color:#82AAFF;">count</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> age </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">60</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">group by</span><span style="color:#A6ACCD;"> gender;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 4、查询所有年龄小于等于35岁员工的年龄和姓名，并对查询结果按年龄升序排序，</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 如果年龄相同按入职时间降序排序</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">,age </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> age </span><span style="color:#89DDFF;">&lt;=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">35</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> age </span><span style="color:#F78C6C;">asc</span><span style="color:#A6ACCD;"> ,entrydate </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;"> ;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 5、查性别为男，年龄在20-40岁(含)以内的前5个员工的信息，对查询的结果按年龄升序，年龄相同按入职时间升序</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> gender </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">男</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> age </span><span style="color:#F78C6C;">between</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">40</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> age </span><span style="color:#F78C6C;">asc</span><span style="color:#A6ACCD;"> ,entrydate </span><span style="color:#F78C6C;">asc</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">limit</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h2 id="sql执行顺序" tabindex="-1">SQL执行顺序 <a class="header-anchor" href="#sql执行顺序" aria-label="Permalink to &quot;SQL执行顺序&quot;">​</a></h2><h3 id="完整执行顺序" tabindex="-1">完整执行顺序 <a class="header-anchor" href="#完整执行顺序" aria-label="Permalink to &quot;完整执行顺序&quot;">​</a></h3><p>在讲解DQL语句的具体语法之前，我们已经讲解了DQL语句的完整语法，及编写顺序，接下来，我们要来说明的是DQL语句在执行时的执行顺序，也就是先执行那一部分，后执行那一部分。</p><blockquote><p>执行顺序： from ... where ... group by ...having ... select ... order by ... limit ...</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206171718273.png" alt="image-20220617171852151" style="zoom:67%;"><h3 id="执行顺序验证" tabindex="-1">执行顺序验证 <a class="header-anchor" href="#执行顺序验证" aria-label="Permalink to &quot;执行顺序验证&quot;">​</a></h3><blockquote><p>验证：查询年龄大于15的员工姓名、年龄，并根据年龄进行升序排序</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">,age </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> age </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">15</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> age </span><span style="color:#F78C6C;">asc</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p>在查询时，我们给emp表起一个别名 e，然后在select 及 where中使用该别名</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> e.name , e.age </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> e.age </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">15</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> age </span><span style="color:#F78C6C;">asc</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p>执行上述SQL语句后，我们看到依然可以正常的查询到结果</p><blockquote><p>此时就说明： from 先执行, 然后where 和 select 执行。那 where 和 select 到底哪个先执行呢?此时，此时我们可以给select后面的字段起别名，然后在 where 中使用这个别名，然后看看是否可以执行成功。</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> e.name ename , e.age eage </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> eage </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">15</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> age </span><span style="color:#F78C6C;">asc</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p>执行上述SQL报错了:</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209192230274.png" alt="image-20220919223007167" style="zoom:80%;"><blockquote><p>由此我们可以得出结论: from 先执行，然后执行 where ， 再执行select 。</p></blockquote><p>接下来，我们再执行如下SQL语句，查看执行效果：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> e.name ename , e.age eage </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> e.age </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">15</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> eage </span><span style="color:#F78C6C;">asc</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p>结果执行成功。 那么也就验证了: order by 是在select 语句之后执行的。</p><blockquote><p>综上所述，我们可以看到DQL语句的执行顺序为： from ... where ... group by ... select... having ... order by ... limit ...</p></blockquote><h2 id="分页查询" tabindex="-1">分页查询 <a class="header-anchor" href="#分页查询" aria-label="Permalink to &quot;分页查询&quot;">​</a></h2><h3 id="基本语法-7" tabindex="-1">基本语法 <a class="header-anchor" href="#基本语法-7" aria-label="Permalink to &quot;基本语法&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> 字段|表达式,...</span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> 表</span></span>
<span class="line"><span style="color:#A6ACCD;">【</span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> 条件】</span></span>
<span class="line"><span style="color:#A6ACCD;">【</span><span style="color:#F78C6C;">group by</span><span style="color:#A6ACCD;"> 分组字段】</span></span>
<span class="line"><span style="color:#A6ACCD;">【</span><span style="color:#F78C6C;">having</span><span style="color:#A6ACCD;"> 条件】</span></span>
<span class="line"><span style="color:#A6ACCD;">【</span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> 排序的字段】</span></span>
<span class="line"><span style="color:#F78C6C;">limit</span><span style="color:#A6ACCD;"> 【offset】, size;</span></span></code></pre></div><ul><li><code>offset要显示条目的起始索引</code>（起始索引从0开始）</li><li><code>size 要显示的条目个数</code></li></ul><h3 id="注意事项-3" tabindex="-1">注意事项 <a class="header-anchor" href="#注意事项-3" aria-label="Permalink to &quot;注意事项&quot;">​</a></h3><blockquote><ul><li><em><em>起始索引从0开始，起始索引 = （查询页码 - 1）</em> 每页显示记录数</em>*</li><li><strong>分页查询是数据库的方言，不同的数据库有不同的实现，MySQL中是LIMIT</strong></li><li><strong>如果查询的是第一页数据，起始索引可以省略，直接简写为 limit 10</strong></li></ul></blockquote><h3 id="实战案例" tabindex="-1">实战案例 <a class="header-anchor" href="#实战案例" aria-label="Permalink to &quot;实战案例&quot;">​</a></h3><p>A. 查询第1页员工数据, 每页展示10条记录</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">limit</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">; </span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">limit</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p>B. 查询第2页员工数据, 每页展示10条记录 --------&gt; offset = (页码-1)*页展示记录数</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">limit</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h2 id="分页问题" tabindex="-1">分页问题 <a class="header-anchor" href="#分页问题" aria-label="Permalink to &quot;分页问题&quot;">​</a></h2><h3 id="问题背景" tabindex="-1">问题背景 <a class="header-anchor" href="#问题背景" aria-label="Permalink to &quot;问题背景&quot;">​</a></h3><blockquote><p>最近部分销售人员反馈在 APP 上查询自己名下客户订单数据时，当往下拉取数据的时候，列表上经常出现重复的订单数据，经过排查，后端代码是通过如下方式来实现数据的分页查询的。</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">limit</span><span style="color:#A6ACCD;"> offset, size </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> create_time </span><span style="color:#F78C6C;">desc</span></span></code></pre></div><blockquote><p><strong>经过细致的分析，这种排序方式，在 app 端分页查询的时候，确实存在问题</strong>。</p></blockquote><p>详细的分析过程如下！</p><h3 id="原因分析-1" tabindex="-1">原因分析 <a class="header-anchor" href="#原因分析-1" aria-label="Permalink to &quot;原因分析&quot;">​</a></h3><p>首先我们初始化一张表，用于模拟订单表查询。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> `</span><span style="color:#82AAFF;">tb_order</span><span style="color:#A6ACCD;">` (</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">order_id</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">bigint</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">11</span><span style="color:#A6ACCD;">) unsigned </span><span style="color:#F78C6C;">NOT NULL</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">create_time</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">datetime</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">DEFAULT</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">NULL</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#C792EA;">PRIMARY KEY</span><span style="color:#A6ACCD;"> (</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">id</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">) ENGINE</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">InnoDB </span><span style="color:#C792EA;">DEFAULT</span><span style="color:#A6ACCD;"> CHARSET</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">utf8mb4;</span></span></code></pre></div><p>然后初始化 5 条数据进去，方便数据分析</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">tb_order</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> (</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">order_id</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">create_time</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">VALUES</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2023-03-03 12:00:01</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">tb_order</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> (</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">order_id</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">create_time</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">VALUES</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2023-03-03 12:00:02</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">tb_order</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> (</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">order_id</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">create_time</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">VALUES</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2023-03-03 12:00:03</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">tb_order</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> (</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">order_id</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">create_time</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">VALUES</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2023-03-03 12:00:04</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">tb_order</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> (</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">order_id</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">create_time</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">VALUES</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2023-03-03 12:00:05</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><p>假设我们每次只查询 2 条数据，并且按照时间倒序来查询，结果如下：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 发起第一页查询</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> tb_order </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> create_time </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">limit</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 第一页查询结果</span></span>
<span class="line"><span style="color:#A6ACCD;">|order_id |   create_time       |</span></span>
<span class="line"><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">        |  </span><span style="color:#F78C6C;">2023</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">12</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">00</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">05</span><span style="color:#A6ACCD;">|</span></span>
<span class="line"><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">        |  </span><span style="color:#F78C6C;">2023</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">12</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">00</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">04</span><span style="color:#A6ACCD;">|</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 发起第二页查询</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> tb_order </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> create_time </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">limit</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 第二页查询结果</span></span>
<span class="line"><span style="color:#A6ACCD;">|order_id |   create_time       |</span></span>
<span class="line"><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">        |  </span><span style="color:#F78C6C;">2023</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">12</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">00</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;">|</span></span>
<span class="line"><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">        |  </span><span style="color:#F78C6C;">2023</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">12</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">00</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">02</span><span style="color:#A6ACCD;">|</span></span></code></pre></div><p>当订单数据没有发生变动的时候，这种查询方式是不会造成出现重复的数据问题。<strong>但是当订单数据发生了变动，比如在查询的时候，突然新增了订单数据，此时的查询结果就完全不一样了</strong>。还是以上面为例，假设在第一次查询的时候，突然新增了一条数据，看看结果如何。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 发起第一页查询</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> tb_order </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> create_time </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">limit</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 第一页查询结果</span></span>
<span class="line"><span style="color:#A6ACCD;">|order_id |   create_time       |</span></span>
<span class="line"><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">        |  </span><span style="color:#F78C6C;">2023</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">12</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">00</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">05</span><span style="color:#A6ACCD;">|</span></span>
<span class="line"><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">        |  </span><span style="color:#F78C6C;">2023</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">12</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">00</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">04</span><span style="color:#A6ACCD;">|</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 新增一条订单数据</span></span>
<span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">tb_order</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> (</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">order_id</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">create_time</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">VALUES</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">6</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2023-03-03 12:00:06</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 发起第二页查询</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> tb_order </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> create_time </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">limit</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 第二页查询结果</span></span>
<span class="line"><span style="color:#A6ACCD;">|order_id |   create_time       |</span></span>
<span class="line"><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">        |  </span><span style="color:#F78C6C;">2023</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">12</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">00</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">04</span><span style="color:#A6ACCD;">|</span></span>
<span class="line"><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">        |  </span><span style="color:#F78C6C;">2023</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">12</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">00</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;">|</span></span></code></pre></div><p><strong>可以很明显的发现，订单ID=4的数据，出现在页面上两次，正常情况下只有一次</strong>！</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202304220956582.png" alt="image-20230422095658512" style="zoom:67%;"><p>上面说到的是新增一条数据，假设删除某条数据，看看结果如何。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 发起第一页查询</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> tb_order </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> create_time </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">limit</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 第一页查询结果</span></span>
<span class="line"><span style="color:#A6ACCD;">|order_id |   create_time       |</span></span>
<span class="line"><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">        |  </span><span style="color:#F78C6C;">2023</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">12</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">00</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">05</span><span style="color:#A6ACCD;">|</span></span>
<span class="line"><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">        |  </span><span style="color:#F78C6C;">2023</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">12</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">00</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">04</span><span style="color:#A6ACCD;">|</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 删除一条订单数据</span></span>
<span class="line"><span style="color:#F78C6C;">delete</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> tb_order </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> order_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 发起第二页查询</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> tb_order </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> create_time </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">limit</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 第二页查询结果</span></span>
<span class="line"><span style="color:#A6ACCD;">|order_id |   create_time       |</span></span>
<span class="line"><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">        |  </span><span style="color:#F78C6C;">2023</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">12</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">00</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">02</span><span style="color:#A6ACCD;">|</span></span>
<span class="line"><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">        |  </span><span style="color:#F78C6C;">2023</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">12</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">00</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">01</span><span style="color:#A6ACCD;">|</span></span></code></pre></div><blockquote><p><strong>可以很明显的发现，删除订单ID=4的数据之后，页面查询结果直接到订单ID=2了，直接跳过订单ID=3了，也就是说订单ID=3的数据展示，丢失了</strong>！</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202304220957120.png" alt="image-20230422095720045" style="zoom:67%;"><p>总结下来，结论如下！</p><blockquote><ul><li><strong>当新增某条数据之后，通过常规的分页查询，列表会出现数据重复的现象</strong>；</li><li><strong>当删除某条数据之后，通过常规的分页查询，列表会出现数据丢失的现象</strong>；</li></ul></blockquote><p>那怎么解决以上的问题呢？办法如下！</p><h3 id="解决方案-1" tabindex="-1">解决方案 <a class="header-anchor" href="#解决方案-1" aria-label="Permalink to &quot;解决方案&quot;">​</a></h3><p>针对上面所说的分页查询方式，我们需要做一些调整，调整办法如下：</p><blockquote><ul><li><strong>第一步：当查询出当页的数据之后，记录下本次拉取的最后一条数据的排序字段值；当发起下一页数据查询的时候，带上这个参数，服务端通过这个参数做过滤条件</strong></li><li><strong>第二步：排序字段值不能出现重复</strong></li></ul></blockquote><p>以上面的新增为例，详细的实践过程如下：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 发起第一页查询</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> tb_order </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> create_time </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">limit</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 第一页查询结果</span></span>
<span class="line"><span style="color:#A6ACCD;">|order_id |   create_time       |</span></span>
<span class="line"><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">        |  </span><span style="color:#F78C6C;">2023</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">12</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">00</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">05</span><span style="color:#A6ACCD;">|</span></span>
<span class="line"><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">        |  </span><span style="color:#F78C6C;">2023</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">12</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">00</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">04</span><span style="color:#A6ACCD;">|</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 新增一条订单数据</span></span>
<span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">tb_order</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> (</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">order_id</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">create_time</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">VALUES</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">6</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2023-03-03 12:00:06</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 发起第二页查询，带上第一页查询的最后一条数据的排序字段值</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> tb_order </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> create_time </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2023-03-03 12:00:04</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> create_time </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">limit</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 第二页查询结果</span></span>
<span class="line"><span style="color:#A6ACCD;">|order_id |   create_time       |</span></span>
<span class="line"><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">        |  </span><span style="color:#F78C6C;">2023</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">12</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">00</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;">|</span></span>
<span class="line"><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">        |  </span><span style="color:#F78C6C;">2023</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">12</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">00</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">02</span><span style="color:#A6ACCD;">|</span></span></code></pre></div><p><strong>此时的查询结果正常，符合预期效果</strong>！</p><p>同样的，以上面的删除为例，详细的实践过程如下：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 发起第一页查询</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> tb_order </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> create_time </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">limit</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 第一页查询结果</span></span>
<span class="line"><span style="color:#A6ACCD;">|order_id |   create_time       |</span></span>
<span class="line"><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">        |  </span><span style="color:#F78C6C;">2023</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">12</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">00</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">05</span><span style="color:#A6ACCD;">|</span></span>
<span class="line"><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">        |  </span><span style="color:#F78C6C;">2023</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">12</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">00</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">04</span><span style="color:#A6ACCD;">|</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 删除一条订单数据</span></span>
<span class="line"><span style="color:#F78C6C;">delete</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> tb_order </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> order_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 发起第二页查询</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> tb_order </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> create_time </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2023-03-03 12:00:04</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> create_time </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">limit</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 第二页查询结果</span></span>
<span class="line"><span style="color:#A6ACCD;">|order_id |   create_time       |</span></span>
<span class="line"><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">        |  </span><span style="color:#F78C6C;">2023</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">12</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">00</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;">|</span></span>
<span class="line"><span style="color:#A6ACCD;">|</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">        |  </span><span style="color:#F78C6C;">2023</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">12</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">00</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">02</span><span style="color:#A6ACCD;">|</span></span></code></pre></div><p>查询结果与预期一致，正常！</p><h3 id="深入思考" tabindex="-1">深入思考 <a class="header-anchor" href="#深入思考" aria-label="Permalink to &quot;深入思考&quot;">​</a></h3><ul><li><strong>选择的排序字段值出现了重复，怎么办</strong>？</li></ul><blockquote><p>在上面我们提到了，<strong>排序字段值不能出现重复</strong>的要求，但是现实的情况是，如果以订单的创建时间来排序，当同一秒多次下单的时候大概率会出现重复，这个时候只能在订单表里面新增一个排序字段，设置全局唯一索引，内容是以时间为基础来生成，比如雪花算法，或者自己写一个基于时间全局自增的算法，确保全局唯一，<strong>最重要的是值的长度必须固定</strong>，订单主键 ID 的生成规则推荐采用此方式，利用主键 ID 来排序效率查询会非常高！</p></blockquote><ul><li><strong>当出现多个排序字段时，如何处理</strong>？</li></ul><blockquote><p>如果是 app 端的查询，不建议设计多字段排序，因为在多字段排序的环境下，服务端在进行多条件的过滤查询时，可能会把有效的数据给过滤掉，如果无法避开，尽量将多个排序字段合并到一个排序字段上，保证数据的查询符合预期。</p></blockquote><h2 id="实战技巧" tabindex="-1">实战技巧 <a class="header-anchor" href="#实战技巧" aria-label="Permalink to &quot;实战技巧&quot;">​</a></h2><h3 id="查找是否存在" tabindex="-1">查找是否存在 <a class="header-anchor" href="#查找是否存在" aria-label="Permalink to &quot;查找是否存在&quot;">​</a></h3><h4 id="目前多数人的写法" tabindex="-1">目前多数人的写法 <a class="header-anchor" href="#目前多数人的写法" aria-label="Permalink to &quot;目前多数人的写法&quot;">​</a></h4><blockquote><p>多次REVIEW代码时，发现如现现象：业务代码中，需要根据一个或多个条件，查询是否存在记录，不关心有多少条记录。普遍的SQL及代码写法如下</p></blockquote><p>SQL写法</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 返回查询到的条目</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">count</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">WHERE</span><span style="color:#A6ACCD;"> a </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">AND</span><span style="color:#A6ACCD;"> b </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span></span></code></pre></div><p>Java写法</p><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> nums </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> xxDao</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">countXxxxByXxx</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">params</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;"> nums </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">)</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">//当存在时，执行这里的代码</span></span>
<span class="line"><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">else</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">//当不存在时，执行这里的代码</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>是不是感觉很OK，没有什么问题</p><h4 id="优化方案" tabindex="-1">优化方案 <a class="header-anchor" href="#优化方案" aria-label="Permalink to &quot;优化方案&quot;">​</a></h4><blockquote><p>SQL不再使用count，而是改用LIMIT 1，让数据库查询时遇到一条就返回，不要再继续查找还有多少条了 业务代码中直接判断是否非空即可</p></blockquote><p>SQL写法</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 查到就返回1，没有就啥也不返回</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">WHERE</span><span style="color:#A6ACCD;"> a </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">AND</span><span style="color:#A6ACCD;"> b </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">LIMIT</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p>Java写法</p><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">Integer</span><span style="color:#A6ACCD;"> exist </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> xxDao</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">existXxxxByXxx</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">params</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">exist </span><span style="color:#89DDFF;">!=</span><span style="color:#A6ACCD;"> NULL</span><span style="color:#89DDFF;">)</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">//当存在时，执行这里的代码</span></span>
<span class="line"><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">else</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">  </span><span style="color:#676E95;font-style:italic;">//当不存在时，执行这里的代码</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h1 id="dql多表查询" tabindex="-1">DQL多表查询 <a class="header-anchor" href="#dql多表查询" aria-label="Permalink to &quot;DQL多表查询&quot;">​</a></h1><h2 id="笛卡儿积" tabindex="-1">笛卡儿积 <a class="header-anchor" href="#笛卡儿积" aria-label="Permalink to &quot;笛卡儿积&quot;">​</a></h2><blockquote><ul><li>只从多张表中查询数据</li><li>笛卡尔积是指在数学中，两个集合A和B的所有组合情况(多表查询要消除笛卡尔积)</li></ul></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209200908167.png" alt="image-20220920090849022" style="zoom:80%;"><h2 id="多表关系⭐" tabindex="-1">多表关系⭐ <a class="header-anchor" href="#多表关系⭐" aria-label="Permalink to &quot;多表关系⭐&quot;">​</a></h2><blockquote><p>多对多关系（Many-to-Many Relationship）：指两个表之间存在的一种关系，其中一个表中的一条记录可以对应另一个表中的多条记录，反之亦然。例如，一个学生可以选择多门课程，一门课程也可以被多个学生选择。在 MySQL 中，需要<strong>使用中间表来实现多对多关系</strong>。</p></blockquote><blockquote><p>自关联关系（Self-Referencing Relationship）：指一个表中的记录与该表中的其他记录存在关联关系。如在一个员工表中，可以存在一个“上级”字段，记录员工的上级是哪个员工。在 MySQL 中，可以使用外键来实现自关联关系</p></blockquote><p>在实际使用中，需要根据具体的业务需求和数据结构来确定表与表之间的关系。</p><h3 id="一对多" tabindex="-1">一对多 <a class="header-anchor" href="#一对多" aria-label="Permalink to &quot;一对多&quot;">​</a></h3><blockquote><p>一对多关系（One-to-Many Relationship）：指两个表之间存在的一种关系，其中一个表的一条记录可以对应另一个表中的多条记录，但另一个表中的一条记录只能对应一个表中的一条记录。例如，一个班级有多个学生，但一个学生只属于一个班级。在 MySQL 中，<strong>可以使用外键来实现一对多关系</strong>。</p></blockquote><blockquote><p>➢案例: 部门与员工的关系</p><p>➢关系: 一个部门对应多个员工，一个员工对应一个部门</p><p>➢实现: 在多的一方建立外键，指向一的一方的主键</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081456950.png" alt="image-20220508145650881" style="zoom:80%;"><h4 id="案例演示" tabindex="-1">案例演示 <a class="header-anchor" href="#案例演示" aria-label="Permalink to &quot;案例演示&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 部门表</span></span>
<span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">dept</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;">    id   </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> auto_increment comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">ID</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">primary key</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">50</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">not null</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">部门名称</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;">)comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">部门表</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 准备数据</span></span>
<span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> dept (id, </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">VALUES</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">研发部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">市场部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">财务部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">销售部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">总经办</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">6</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">人事部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 员工表</span></span>
<span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">emp</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;">    id  </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> auto_increment comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">ID</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">primary key</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">50</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">not null</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">姓名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    age  </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">年龄</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    job </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">职位</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    salary </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">薪资</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    entrydate </span><span style="color:#C792EA;">date</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">入职时间</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    managerid </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">直属领导ID</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    dept_id </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">部门ID</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;">)comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">员工表</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 准备数据</span></span>
<span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> emp (id, </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">, age, job,salary, entrydate, managerid, dept_id) </span><span style="color:#F78C6C;">VALUES</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">金庸</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">66</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">总裁</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">20000</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2000-01-01</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张无忌</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">项目经理</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">12500</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2005-12-05</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">杨逍</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">33</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">开发</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">8400</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2000-11-03</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">韦一笑</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">48</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">开发</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">11000</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2002-02-05</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">常遇春</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">43</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">开发</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">10500</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2004-09-07</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">17</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">陈友谅</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">42</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2000</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2011-10-12</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 添加外键(可选)</span></span>
<span class="line"><span style="color:#F78C6C;">alter</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">add</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">constraint</span><span style="color:#A6ACCD;"> fk_emp_dept_id</span></span>
<span class="line"><span style="color:#C792EA;">foreign key</span><span style="color:#A6ACCD;"> (dept_id)</span></span>
<span class="line"><span style="color:#C792EA;">references</span><span style="color:#A6ACCD;"> dept(id) </span><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> cascade </span><span style="color:#C792EA;">on delete cascade</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h4 id="查询测试" tabindex="-1">查询测试 <a class="header-anchor" href="#查询测试" aria-label="Permalink to &quot;查询测试&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 内连接演示</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 1. 查询每一个员工的姓名 , 及关联的部门的名称 (隐式内连接实现)</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 表结构: emp , dept</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 连接条件: emp.dept_id = dept.id</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> e.name 姓名,d.name 部门 </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e , dept d</span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> e.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> d.id;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302270944549.png" alt="image-20230227094447428" style="zoom:80%;"><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 2. 查询每一个员工的姓名 , 及关联的部门的名称 (显式内连接实现)</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- INNER JOIN ... ON ...</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 表结构: emp , dept</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 连接条件: emp.dept_id = dept.id</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> e.name, d.name </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e</span></span>
<span class="line"><span style="color:#F78C6C;">inner join</span><span style="color:#A6ACCD;"> dept d  </span><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> e.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> d.id;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> e.name, d.name </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e</span></span>
<span class="line"><span style="color:#F78C6C;">join</span><span style="color:#A6ACCD;"> dept d  </span><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> e.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> d.id;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081433282.png" alt="image-20220508143308216" style="zoom:80%;"><h3 id="多对多⭐" tabindex="-1">多对多⭐ <a class="header-anchor" href="#多对多⭐" aria-label="Permalink to &quot;多对多⭐&quot;">​</a></h3><blockquote><p>➢案例: 学生与课程的关系</p><p>➢关系: 一个学生可以选修多门课程，一门课程也可以供多个学生选择</p><p>➢实现: <strong>建立第三张中间表</strong>，中间表至少包含两个外键，分别关联两方主键</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220120162752102.png" alt="image-20220120162752102" style="zoom:80%;"><h4 id="案例演示-1" tabindex="-1">案例演示 <a class="header-anchor" href="#案例演示-1" aria-label="Permalink to &quot;案例演示&quot;">​</a></h4><p>学生表</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">student</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;">    id </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> auto_increment </span><span style="color:#C792EA;">primary key</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">主键ID</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">姓名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F78C6C;">no</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">学号</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">学生表</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F78C6C;">insert into</span><span style="color:#A6ACCD;"> student </span><span style="color:#F78C6C;">values</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">黛绮丝</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2000100101</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">谢逊</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2000100102</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">殷天正</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2000100103</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">韦一笑</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2000100104</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><p>课程表</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">course</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;">    id </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> auto_increment </span><span style="color:#C792EA;">primary key</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">主键ID</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">课程名称</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">课程表</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">insert into</span><span style="color:#A6ACCD;"> course </span><span style="color:#F78C6C;">values</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Java</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">), </span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">PHP</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">), </span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;"> , </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">MySQL</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">) , </span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Hadoop</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><p>中间连接表</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">student_course</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;">    id </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> auto_increment comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">主键</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">primary key</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    studentid </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">not null</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">学生ID</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    courseid  </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">not null</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">课程ID</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">constraint</span><span style="color:#A6ACCD;"> fk_courseid </span><span style="color:#C792EA;">foreign key</span><span style="color:#A6ACCD;"> (courseid) </span><span style="color:#C792EA;">references</span><span style="color:#A6ACCD;"> course (id),</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">constraint</span><span style="color:#A6ACCD;"> fk_studentid </span><span style="color:#C792EA;">foreign key</span><span style="color:#A6ACCD;"> (studentid) </span><span style="color:#C792EA;">references</span><span style="color:#A6ACCD;"> student (id)</span></span>
<span class="line"><span style="color:#A6ACCD;">)comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">学生课程中间表</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F78C6C;">insert into</span><span style="color:#A6ACCD;"> student_course </span><span style="color:#F78C6C;">values</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081458876.png" alt="image-20220508145839805" style="zoom:80%;"><h4 id="查询测试-1" tabindex="-1">查询测试 <a class="header-anchor" href="#查询测试-1" aria-label="Permalink to &quot;查询测试&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> s.name , s.no , c.name </span><span style="color:#F78C6C;">from</span></span>
<span class="line"><span style="color:#A6ACCD;">student s , student_course sc , course c</span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> s.id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> sc.studentid </span><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> sc.courseid </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> c.id ;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081440055.png" alt="image-20220508144019988" style="zoom:80%;"><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> s.name 姓名,s.no 学号, c.name 学科 </span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> student s</span></span>
<span class="line"><span style="color:#F78C6C;">join</span><span style="color:#A6ACCD;"> student_course sc</span></span>
<span class="line"><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> s.id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> sc.studentid</span></span>
<span class="line"><span style="color:#F78C6C;">join</span><span style="color:#A6ACCD;"> course c</span></span>
<span class="line"><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> sc.courseid </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> c.id;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206171454358.png" alt="image-20220617145432286" style="zoom:80%;"><h3 id="一对一" tabindex="-1">一对一 <a class="header-anchor" href="#一对一" aria-label="Permalink to &quot;一对一&quot;">​</a></h3><blockquote><p>一对一关系（One-to-One Relationship）：指两个表之间存在的一种关系，其中一个表的一条记录最多只能对应另一个表中的一条记录，反之亦然。例如，一个人只有一个身份证号码，一个身份证号码只能对应一个人。在 MySQL 中，<strong>可以使用外键来实现一对一关系</strong>。</p></blockquote><blockquote><p>➢案例: 用户与用户详情的关系</p><p>➢关系: 一对一关系，多用于单表拆分，将一张表的基础字段放在一张表中，其他详情字段放在另一张表中</p><p>➢实现: 在任意一方加入外键，关联另外一方的主键，并且设置外键为唯一的(UNIQUE)</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220120162730764.png" alt="image-20220120162730764" style="zoom:80%;"><h4 id="案例演示-2" tabindex="-1">案例演示 <a class="header-anchor" href="#案例演示-2" aria-label="Permalink to &quot;案例演示&quot;">​</a></h4><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206171456720.png" alt="image-20220617145653641" style="zoom:80%;"><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">tb_user</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;">    id </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> auto_increment </span><span style="color:#C792EA;">primary key</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">主键ID</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">姓名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    age </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">年龄</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    gender </span><span style="color:#C792EA;">char</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1: 男 , 2: 女</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    phone </span><span style="color:#C792EA;">char</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">11</span><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">手机号</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">用户基本信息表</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F78C6C;">insert into</span><span style="color:#A6ACCD;"> tb_user(id, </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">, age, gender, phone) </span><span style="color:#F78C6C;">values</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">黄渤</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">45</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">18800001111</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">冰冰</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">35</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">18800002222</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">码云</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">55</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">18800008888</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">李彦宏</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">50</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">18800009999</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">tb_user_edu</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;">    id </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> auto_increment </span><span style="color:#C792EA;">primary key</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">主键ID</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    degree </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">学历</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    major </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">50</span><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">专业</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    primaryschool </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">50</span><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">小学</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    middleschool </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">50</span><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">中学</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    university </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">50</span><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">大学</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    userid </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">unique</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">用户ID</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#89DDFF;">    </span><span style="color:#676E95;font-style:italic;">-- 外键</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">constraint</span><span style="color:#A6ACCD;"> fk_userid </span><span style="color:#C792EA;">foreign key</span><span style="color:#A6ACCD;"> (userid) </span><span style="color:#C792EA;">references</span><span style="color:#A6ACCD;"> tb_user(id)</span></span>
<span class="line"><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">用户教育信息表</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F78C6C;">insert into</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">tb_user_edu(id, degree, major, primaryschool, middleschool, university, userid) </span><span style="color:#F78C6C;">values</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">本科</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">舞蹈</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">静安区第一小学</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">静安区第一中学</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">北京舞蹈学院</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">硕士</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">表演</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">朝阳区第一小学</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">朝阳区第一中学</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">北京电影学院</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">本科</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">英语</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">杭州市第一小学</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">杭州市第一中学</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">杭州师范大学</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">本科</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">应用数学</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">阳泉第一小学</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">阳泉区第一中学</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">清华大学</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h4 id="查询测试-2" tabindex="-1">查询测试 <a class="header-anchor" href="#查询测试-2" aria-label="Permalink to &quot;查询测试&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> u.id,u.name,e.degree,e.university</span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> tb_user u</span></span>
<span class="line"><span style="color:#F78C6C;">inner join</span><span style="color:#A6ACCD;"> tb_user_edu e</span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> u.id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> e.userid;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206171502503.png" alt="image-20220617150217423" style="zoom:80%;"><h2 id="多表查询分类" tabindex="-1">多表查询分类 <a class="header-anchor" href="#多表查询分类" aria-label="Permalink to &quot;多表查询分类&quot;">​</a></h2><h3 id="内连接" tabindex="-1">内连接 <a class="header-anchor" href="#内连接" aria-label="Permalink to &quot;内连接&quot;">​</a></h3><blockquote><p>内连接（INNER JOIN）：内连接会根据两个表之间的匹配关系来返回结果集，<strong>只会返回符合匹配条件的记录</strong>。内连接使用 <code>INNER JOIN</code> 关键字进行连接，也可以使用 <code>JOIN</code> 关键字进行连接。示例如下：</p></blockquote><p>隐式内连接</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> 字段列表 </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> 表1,表2 </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> 条件...；</span></span></code></pre></div><p>显式内连接</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> 字段列表 </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> 表1 </span><span style="color:#F78C6C;">inner join</span><span style="color:#A6ACCD;"> 表2 </span><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> 连接条件...；</span></span></code></pre></div><p>内连接查询的是两张表交集的部分</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081515582.png" alt="image-20220508151511511" style="zoom:80%;"><p>内连接演示</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 内连接演示</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 1. 查询每一个员工的姓名 , 及关联的部门的名称 (隐式内连接实现)</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 表结构: emp , dept ，连接条件: emp.dept_id = dept.id</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 隐式内连接</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> emp.name , dept.name </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp , dept</span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> emp.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> dept.id ;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 隐式内连接，使用别名⭐</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> e.name,d.name </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e , dept d</span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> e.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> d.id;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081519710.png" alt="image-20220508151921639" style="zoom:80%;"><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 2. 查询每一个员工的姓名 , 及关联的部门的名称 (显式内连接实现)</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- INNER JOIN ... ON ...</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 表结构: emp , dept，连接条件: emp.dept_id = dept.id</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 显式</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> e.name, d.name </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e</span></span>
<span class="line"><span style="color:#F78C6C;">inner join</span><span style="color:#A6ACCD;"> dept d  </span><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> e.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> d.id;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 显式</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> e.name, d.name </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e</span></span>
<span class="line"><span style="color:#F78C6C;">join</span><span style="color:#A6ACCD;"> dept d  </span><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> e.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> d.id;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081522718.png" alt="image-20220508152227650" style="zoom:80%;"><h3 id="外连接" tabindex="-1">外连接 <a class="header-anchor" href="#外连接" aria-label="Permalink to &quot;外连接&quot;">​</a></h3><p>外连接查询语法:</p><h4 id="左外连接" tabindex="-1">左外连接 <a class="header-anchor" href="#左外连接" aria-label="Permalink to &quot;左外连接&quot;">​</a></h4><blockquote><p>左连接<strong>会返回左表中所有的记录以及符合匹配条件的右表中的记录。如果右表中没有匹配的记录，则会返回 NULL 值</strong>。左连接使用 <code>LEFT JOIN</code> 关键字进行连接。</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> 字段列表 </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> 表1 left [outer] </span><span style="color:#F78C6C;">join</span><span style="color:#A6ACCD;"> 表2  </span><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;">  条件.....;</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 外连接演示</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 1. 查询emp表的所有数据, 和对应的部门信息(左外连接)</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 表结构: emp, dept，连接条件: emp.dept_id = dept.id</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> e.</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">, d.name </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e</span></span>
<span class="line"><span style="color:#F78C6C;">left outer join</span><span style="color:#A6ACCD;"> dept d</span></span>
<span class="line"><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> e.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> d.id;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> e.</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">, d.name </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e</span></span>
<span class="line"><span style="color:#F78C6C;">left join</span><span style="color:#A6ACCD;"> dept d</span></span>
<span class="line"><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> e.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> d.id;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081533532.png" alt="image-20220508153341452" style="zoom:80%;"><h4 id="右外连接" tabindex="-1">右外连接 <a class="header-anchor" href="#右外连接" aria-label="Permalink to &quot;右外连接&quot;">​</a></h4><blockquote><p>右连接与左连接类似，不同之处在于右连接会返回右表中的所有记录以及符合匹配条件的左表中的记录。如果左表中没有匹配的记录，则会返回 NULL 值。右连接使用 <code>RIGHT JOIN</code> 关键字进行连接。</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> 字段列表 </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> 表1 right [outer] </span><span style="color:#F78C6C;">join</span><span style="color:#A6ACCD;"> 表2  </span><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;">  条件.....;</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 2. 查询dept表的所有数据, 和对应的员工信息(右外连接)</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> d.</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">, e.</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e</span></span>
<span class="line"><span style="color:#F78C6C;">right outer join</span><span style="color:#A6ACCD;"> dept d</span></span>
<span class="line"><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> e.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> d.id;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> d.</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">, e.</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> dept d</span></span>
<span class="line"><span style="color:#F78C6C;">right outer join</span><span style="color:#A6ACCD;"> emp e</span></span>
<span class="line"><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> e.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> d.id;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081534130.png" alt="image-20220508153416051" style="zoom:80%;"><h3 id="自连接" tabindex="-1">自连接 <a class="header-anchor" href="#自连接" aria-label="Permalink to &quot;自连接&quot;">​</a></h3><h4 id="基本语法-8" tabindex="-1">基本语法 <a class="header-anchor" href="#基本语法-8" aria-label="Permalink to &quot;基本语法&quot;">​</a></h4><blockquote><p>MySQL 自连接（Self Join）是指在一个表中进行连接操作，将该表视为两个独立的表进行连接操作。自连接通常用于需要在同一个表中进行相关数据查询的情况。</p></blockquote><blockquote><p>自连接操作需要在 <code>FROM</code> 子句中使用两个表别名（alias），一个别名用于表示连接的源表，另一个别名用于表示连接的目标表。接下来需要使用连接条件，将两个表连接起来。连接条件通常是基于两个表的字段进行连接，而这两个字段通常具有相关性。例如：在一张表上找两遍：例如找一张表上的一级标题对应的多个二级标题，或者查询员工名和上级的名称</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209200932467.png" alt="image-20220920093259273" style="zoom:80%;"><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> 字段列表 </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> 表A  别名A </span><span style="color:#F78C6C;">join</span><span style="color:#A6ACCD;"> 表A 别名B  </span><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;">  条件.....;</span></span></code></pre></div><p>例如，在一个员工表中，可以存在一个“上级”字段，记录员工的上级是哪个员工。如果需要查询每个员工的姓名和上级的姓名，可以使用自连接来实现。示例如下：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> e.name, m.name </span><span style="color:#F78C6C;">AS</span><span style="color:#A6ACCD;"> manager_name</span></span>
<span class="line"><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> employee e</span></span>
<span class="line"><span style="color:#F78C6C;">LEFT JOIN</span><span style="color:#A6ACCD;"> employee m </span><span style="color:#F78C6C;">ON</span><span style="color:#A6ACCD;"> e.manager_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> m.id;</span></span></code></pre></div><blockquote><p>上面的 SQL 查询语句使用了自连接，将 <code>employee</code> 表连接两次，使用别名 <code>e</code> 和 <code>m</code> 分别表示源表和目标表。连接条件是 <code>e.manager_id = m.id</code>，表示 <code>e</code> 表中的 <code>manager_id</code> 字段与 <code>m</code> 表中的 <code>id</code> 字段相等。查询结果包括员工的姓名和上级的姓名，其中上级的姓名使用了别名 <code>manager_name</code> 进行重命名。注意，在使用自连接时，需要特别注意查询的字段、别名和连接条件等方面的设置，以确保查询结果正确无误。</p></blockquote><h4 id="案例演示-3" tabindex="-1">案例演示 <a class="header-anchor" href="#案例演示-3" aria-label="Permalink to &quot;案例演示&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 1. 查询员工 及其 所属领导的名字</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> a.name , b.name </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp a , emp b</span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> a.managerid </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> b.id;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081537641.png" alt="image-20220508153750566" style="zoom:80%;"><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 2. 查询所有员工 emp 及其领导的名字 emp , 如果员工没有领导, 也需要查询出来</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 表结构: emp a , emp b</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> a.name </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">员工</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, b.name </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">领导</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp a </span><span style="color:#F78C6C;">left join</span><span style="color:#A6ACCD;"> emp b</span></span>
<span class="line"><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> a.managerid </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> b.id;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081538705.png" alt="image-20220508153849629" style="zoom:80%;"><h2 id="联合查询" tabindex="-1">联合查询 <a class="header-anchor" href="#联合查询" aria-label="Permalink to &quot;联合查询&quot;">​</a></h2><blockquote><p>union 联合、合并：将多条查询语句的结果合并成一个结果</p><p>应用场景：<strong>要查询的结果来自于多个表，且多个表没有直接的连接关系，但查询的信息一致时</strong></p></blockquote><p>特点：★</p><blockquote><p>1、<strong>要求多条查询语句的查询列数是一致的！</strong></p><p>2、<strong>要求多条查询语句的查询的每一列的类型和顺序最好一致</strong></p><p>3、<strong>union关键字默认去重，如果使用union all 可以包含重复项</strong></p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> 字段|常量|表达式|函数 【</span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> 表】 【</span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> 条件】 </span><span style="color:#F78C6C;">union</span><span style="color:#A6ACCD;"> 【all】</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> 字段|常量|表达式|函数 【</span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> 表】 【</span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> 条件】 </span><span style="color:#F78C6C;">union</span><span style="color:#A6ACCD;"> 【all】</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> 字段|常量|表达式|函数 【</span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> 表】 【</span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> 条件】 </span><span style="color:#F78C6C;">union</span><span style="color:#A6ACCD;">  【all】</span></span>
<span class="line"><span style="color:#A6ACCD;">.....</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> 字段|常量|表达式|函数 【</span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> 表】 【</span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> 条件】</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 查询部门编号</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">90或邮箱包含a的员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> employees </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> email </span><span style="color:#F78C6C;">like</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%a%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">or</span><span style="color:#A6ACCD;"> department_id</span><span style="color:#89DDFF;">&gt;</span><span style="color:#F78C6C;">90</span></span>
<span class="line"><span style="color:#A6ACCD;"># 将or替换成union联合查询</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> employees </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> email </span><span style="color:#F78C6C;">like</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%a%</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#F78C6C;">union</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> employees </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> department_id </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">90</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20210817191633086.png" alt="image-20210817191633086" style="zoom:67%;"><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#案例：查询中国用户中男性的信息以及外国用户中年男性的用户信息</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> id,cname </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> t_ca </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> csex</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">男</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#F78C6C;">union all</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> t_id,tname </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> t_ua </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> tGender</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">male</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h2 id="子查询⭐" tabindex="-1">子查询⭐ <a class="header-anchor" href="#子查询⭐" aria-label="Permalink to &quot;子查询⭐&quot;">​</a></h2><h3 id="基本语法-9" tabindex="-1">基本语法 <a class="header-anchor" href="#基本语法-9" aria-label="Permalink to &quot;基本语法&quot;">​</a></h3><blockquote><p>SQL语句中嵌套SELECT语句，称为嵌套查询，又称子查询。子查询外部的语句可以是INSERT / UPDATE / DELETE / SELECT 的任何一个。</p></blockquote><h4 id="子查询分类" tabindex="-1">子查询分类 <a class="header-anchor" href="#子查询分类" aria-label="Permalink to &quot;子查询分类&quot;">​</a></h4><h4 id="结果不同" tabindex="-1">结果不同 <a class="header-anchor" href="#结果不同" aria-label="Permalink to &quot;结果不同&quot;">​</a></h4><blockquote><p>注意：<strong>结果集是指括号内子查询的结果集，而不是最终的结果集，这非常重要</strong></p></blockquote><blockquote><ul><li>标量子查询（子查询结果为单个值）</li><li>列子查询（子查询结果为一列）</li><li>行子查询（子查询结果为一行）</li><li>表子查询（子查询结果为多行多列）</li></ul></blockquote><h4 id="位置不同" tabindex="-1">位置不同 <a class="header-anchor" href="#位置不同" aria-label="Permalink to &quot;位置不同&quot;">​</a></h4><blockquote><p>子查询可以放在from后面、select后面、where后面、having后面</p></blockquote><h3 id="标量子查询" tabindex="-1">标量子查询 <a class="header-anchor" href="#标量子查询" aria-label="Permalink to &quot;标量子查询&quot;">​</a></h3><blockquote><p>子查询返回的结果是单个值（数字、字符串、日期等），最简单的形式，这种子查询成为标量子查询</p><p>常用的操作符：= &lt;&gt; &gt; &gt;= &lt; &lt;=</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 标量子查询</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 1. 查询 &quot;销售部&quot; 的所有员工信息</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- a. 查询 &quot;销售部&quot; 部门ID</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> id </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> dept </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">销售部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- b. 根据销售部部门ID, 查询员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> id </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> dept </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">销售部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081546492.png" alt="image-20220508154609412" style="zoom:80%;"><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 2. 查询在 &quot;方东白&quot; 入职之后的员工信息</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- a. 查询 方东白 的入职日期</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> entrydate </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">方东白</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- b. 查询指定入职日期之后入职的员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> entrydate </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> entrydate </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">方东白</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081546984.png" alt="image-20220508154654910" style="zoom:80%;"><h3 id="列子查询" tabindex="-1">列子查询 <a class="header-anchor" href="#列子查询" aria-label="Permalink to &quot;列子查询&quot;">​</a></h3><blockquote><p>子查询返回的结果是一列（可以是多行），这种子查询称为列子查询</p><p>常用的操作符：IN 、NOT IN 、 ANY 、SOME 、 ALL</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209200942310.png" alt="image-20220920094230138" style="zoom:80%;"><p>案例1：查询 &quot;销售部&quot; 和 &quot;市场部&quot; 的所有员工信息</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 1. 查询 &quot;销售部&quot; 和 &quot;市场部&quot; 的所有员工信息</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- a. 查询 &quot;销售部&quot; 和 &quot;市场部&quot; 的部门ID</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> id </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> dept </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">销售部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">or</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">市场部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- b. 根据部门ID, 查询员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> dept_id </span><span style="color:#F78C6C;">in</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> id </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> dept </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">销售部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">or</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">市场部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081548717.png" alt="image-20220508154820632" style="zoom:80%;"><p>案例2：查询比财务部 所有人工资都高的员工信息</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- a. 查询所有财务部人员工资</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> id </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> dept </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">财务部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> salary </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> id </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> dept </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">财务部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- b. 比 财务部 所有人工资都高的员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> salary </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> all </span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> salary </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> id </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> dept </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">财务部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">));</span></span></code></pre></div><p>案例3：查询比研发部其中任意一人工资高的员工信息</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 3. 查询比研发部其中任意一人工资高的员工信息</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- a. 查询研发部所有人工资</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> salary </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> id </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> dept </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">研发部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- b. 比研发部其中任意一人工资高的员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> salary </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> some </span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> salary </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> id </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> dept </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">研发部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">));</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081550921.png" alt="image-20220508155018834" style="zoom:80%;"><h3 id="行子查询" tabindex="-1">行子查询 <a class="header-anchor" href="#行子查询" aria-label="Permalink to &quot;行子查询&quot;">​</a></h3><blockquote><p>子查询返回的结果是一行（可以是多列），这种子查询称为行子查询。常用的操作符：= 、&lt;&gt; 、IN 、NOT IN</p></blockquote><p>案例：查询与 &quot;张无忌&quot; 的薪资及直属领导相同的员工信息;</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 行子查询</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 1. 查询与 &quot;张无忌&quot; 的薪资及直属领导相同的员工信息 ;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- a. 查询 &quot;张无忌&quot; 的薪资及直属领导</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> salary, managerid </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张无忌</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- b. 查询与 &quot;张无忌&quot; 的薪资及直属领导相同的员工信息 ;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> (salary,managerid) </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> salary, managerid </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张无忌</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081551285.png" alt="image-20220508155128206" style="zoom:80%;"><h3 id="表子查询" tabindex="-1">表子查询 <a class="header-anchor" href="#表子查询" aria-label="Permalink to &quot;表子查询&quot;">​</a></h3><blockquote><p>子查询返回的结果是多行多列(相当于一张表)，这种子查询称为表子查询。</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 表子查询</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 1. 查询与 &quot;鹿杖客&quot; , &quot;宋远桥&quot; 的职位和薪资相同的员工信息</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- a. 查询 &quot;鹿杖客&quot; , &quot;宋远桥&quot; 的职位和薪资</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> job, salary </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">鹿杖客</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">or</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">宋远桥</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- b. 查询与 &quot;鹿杖客&quot; , &quot;宋远桥&quot; 的职位和薪资相同的员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> (job,salary) </span><span style="color:#F78C6C;">in</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> job, salary </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">鹿杖客</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">or</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">宋远桥</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> );</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081602674.png" alt="image-20220508160204592" style="zoom:80%;"><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 2. 查询入职日期是 &quot;2006-01-01&quot; 之后的员工信息 , 及其部门信息</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- a. 入职日期是 &quot;2006-01-01&quot; 之后的员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> entrydate </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2006-01-01</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- b. 查询这部分员工, 对应的部门信息;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> e.</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">, d.</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> entrydate </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2006-01-01</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">) e</span></span>
<span class="line"><span style="color:#F78C6C;">left join</span><span style="color:#A6ACCD;"> dept d </span><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> e.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> d.id ;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081602434.png" alt="image-20220508160239350" style="zoom:80%;"><h2 id="综合案例⭐" tabindex="-1">综合案例⭐ <a class="header-anchor" href="#综合案例⭐" aria-label="Permalink to &quot;综合案例⭐&quot;">​</a></h2><h3 id="准备数据" tabindex="-1">准备数据 <a class="header-anchor" href="#准备数据" aria-label="Permalink to &quot;准备数据&quot;">​</a></h3><blockquote><p>在这个案例中，我们主要运用上面所讲解的多表查询的语法，完成以下的12个需求即可，而这里主要涉及到的表就三张：emp员工表、dept部门表、salgrade薪资等级表</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">salgrade</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;">    grade </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    losal </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    hisal </span><span style="color:#C792EA;">int</span></span>
<span class="line"><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">薪资等级表</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">insert into</span><span style="color:#A6ACCD;"> salgrade </span><span style="color:#F78C6C;">values</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">3000</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">3001</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">5000</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">5001</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">8000</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">8001</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">10000</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">10001</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">15000</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">6</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">15001</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">20000</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">7</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">20001</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">25000</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">8</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">25001</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">30000</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="需求分析" tabindex="-1">需求分析 <a class="header-anchor" href="#需求分析" aria-label="Permalink to &quot;需求分析&quot;">​</a></h3><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302271053599.png" alt="image-20230227105345439" style="zoom:80%;"><h3 id="查询实现" tabindex="-1">查询实现 <a class="header-anchor" href="#查询实现" aria-label="Permalink to &quot;查询实现&quot;">​</a></h3><blockquote><p>1.查询员工的姓名、年龄、职位、部门信息。</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 1. 查询员工的姓名、年龄、职位、部门信息 （隐式内连接）</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 表: emp , dept，连接条件: emp.dept_id = dept.id</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> e.name , e.age , e.job , d.name</span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e , dept d </span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> e.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> d.id;</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> e.name , e.age , e.job , d.name</span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e</span></span>
<span class="line"><span style="color:#F78C6C;">join</span><span style="color:#A6ACCD;"> dept d</span></span>
<span class="line"><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> e.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> d.id;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209200953454.png" alt="image-20220920095328282" style="zoom:80%;"><blockquote><p>2.查询年龄小于30岁的员工姓名、年龄、职位、部门信息</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 2. 查询年龄小于30岁的员工的姓名、年龄、职位、部门信息（显式内连接）</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 表: emp , dept，连接条件: emp.dept_id = dept.id</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> e.name , e.age , e.job , d.name </span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e</span></span>
<span class="line"><span style="color:#F78C6C;">inner join</span><span style="color:#A6ACCD;"> dept d </span></span>
<span class="line"><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> e.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> d.id </span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> e.age </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">30</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081933148.png" alt="image-20220508193343055" style="zoom:80%;"><blockquote><p>3.查询拥有员工的部门ID、部门名称。</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 3. 查询拥有员工的部门ID、部门名称</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 表: emp , dept，连接条件: emp.dept_id = dept.id</span></span>
<span class="line"><span style="color:#F78C6C;">select distinct</span><span style="color:#A6ACCD;"> d.id , d.name </span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e , dept d</span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> e.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> d.id;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081934775.png" alt="image-20220508193419670" style="zoom:80%;"><blockquote><p>4.查询所有年龄大于40岁的员工, 及其归属的部门名称; 如果员工没有分配部门, 也需要展示出来。</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 4. 查询所有年龄大于40岁的员工, 及其归属的部门名称; 如果员工没有分配部门, 也需要展示出来</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 表: emp , dept</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 连接条件: emp.dept_id = dept.id</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 外连接</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> e.</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">, d.name </span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e </span></span>
<span class="line"><span style="color:#F78C6C;">left join</span><span style="color:#A6ACCD;"> dept d</span></span>
<span class="line"><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> e.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> d.id </span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> e.age </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">40</span><span style="color:#A6ACCD;"> ;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081934298.png" alt="image-20220508193451202" style="zoom:80%;"><blockquote><p>5.查询所有员工的工资等级。</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 5. 查询所有员工的工资等级</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 表: emp , salgrade</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 连接条件 : emp.salary &gt;= salgrade.losal and emp.salary &lt;= salgrade.hisal</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> e.</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> , s.grade , s.losal, s.hisal </span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e , salgrade s</span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> e.salary </span><span style="color:#89DDFF;">&gt;=</span><span style="color:#A6ACCD;"> s.losal </span><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> e.salary </span><span style="color:#89DDFF;">&lt;=</span><span style="color:#A6ACCD;"> s.hisal;</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> e.</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> , s.grade , s.losal, s.hisal </span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e , salgrade s</span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> e.salary </span></span>
<span class="line"><span style="color:#F78C6C;">between</span><span style="color:#A6ACCD;"> s.losal </span><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> s.hisal;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081935420.png" alt="image-20220508193534321" style="zoom:80%;"><blockquote><p>6.查询 &quot;研发部&quot; 所有员工的信息及工资等级。</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 6. 查询 &quot;研发部&quot; 所有员工的信息及 工资等级</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 表: emp , salgrade , dept</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 连接条件 : emp.salary between salgrade.losal and salgrade.hisal , emp.dept_id = dept.id</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 查询条件 : dept.name = &#39;研发部&#39;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> e.</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">, s.grade</span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e,</span></span>
<span class="line"><span style="color:#A6ACCD;">     dept d,</span></span>
<span class="line"><span style="color:#A6ACCD;">     salgrade s</span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> e.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> d.id</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> (e.salary </span><span style="color:#F78C6C;">between</span><span style="color:#A6ACCD;"> s.losal </span><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> s.hisal)</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> d.name </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">研发部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081942454.png" alt="image-20220508194204331" style="zoom:80%;"><blockquote><p>7.查询 &quot;研发部&quot; 员工的平均工资。</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 7. 查询 &quot;研发部&quot; 员工的平均工资</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 表: emp , dept</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 连接条件 :  emp.dept_id = dept.id</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">avg</span><span style="color:#A6ACCD;">(e.salary) </span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e, dept d</span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> e.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> d.id </span><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> d.name </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">研发部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081941061.png" alt="image-20220508194132971" style="zoom:80%;"><blockquote><p>8.查询工资比 &quot;灭绝&quot; 高的员工信息（标量子查询）。</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 8. 查询工资比 &quot;灭绝&quot; 高的员工信息。</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- a. 查询 &quot;灭绝&quot; 的薪资</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> salary </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">灭绝</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- b. 查询比她工资高的员工数据</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> salary </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> salary </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">灭绝</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081940275.png" alt="image-20220508194021181" style="zoom:80%;"><blockquote><p>9.查询比平均薪资高的员工信息。</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 9. 查询比平均薪资高的员工信息</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- a. 查询员工的平均薪资</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">avg</span><span style="color:#A6ACCD;">(salary) </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- b. 查询比平均薪资高的员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> salary </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">avg</span><span style="color:#A6ACCD;">(salary) </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp);</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081939556.png" alt="image-20220508193950464" style="zoom:80%;"><blockquote><p>10.查询低于本部门平均工资的员工信息。</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 10. 查询低于本部门平均工资的员工信息</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- a. 查询指定部门平均薪资  1</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">avg</span><span style="color:#A6ACCD;">(e1.salary) </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e1 </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> e1.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">avg</span><span style="color:#A6ACCD;">(e1.salary) </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e1 </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> e1.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- b. 查询低于本部门平均工资的员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e2</span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> e2.salary</span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;">( </span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">avg</span><span style="color:#A6ACCD;">(e1.salary) </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e1 </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> e1.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> e2.dept_id);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 将平均薪资放在最后一列(可选，就是验证下结果)</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">, (</span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">avg</span><span style="color:#A6ACCD;">(e1.salary) </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e1 </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> e1.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> e2.dept_id) </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">平均</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e2</span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> e2.salary </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">avg</span><span style="color:#A6ACCD;">(e1.salary) </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e1 </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> e1.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> e2.dept_id);</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081938722.png" alt="image-20220508193848628" style="zoom:80%;"><blockquote><p>11.查询所有的部门信息, 并统计部门的员工人数⭐。</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 11. 查询所有的部门信息, 并统计部门的员工人数</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- a.子查询语句</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> id,</span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> dept;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">count</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- b.实现子查询</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> d.id, d.name , (</span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">count</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp e </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> e.dept_id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> d.id ) </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">人数</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> dept d;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081937817.png" alt="image-20220508193734726" style="zoom:80%;"><blockquote><p>12.查询所有学生的选课情况, 展示出学生名称, 学号, 课程名称</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 12. 查询所有学生的选课情况, 展示出学生名称, 学号, 课程名称</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 表: student , course , student_course</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 连接条件: student.id = student_course.studentid , course.id = student_course.courseid</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> s.name, s.no, c.name</span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> student s,</span></span>
<span class="line"><span style="color:#A6ACCD;">     student_course sc,</span></span>
<span class="line"><span style="color:#A6ACCD;">     course c</span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> s.id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> sc.studentid</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> sc.courseid </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> c.id;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5/202205081936157.png" alt="image-20220508193606052" style="zoom:80%;"><p>备注：以上需求的实现方式可能会很多, SQL写法也有很多，只要能满足我们的需求，查询出符合条件的记录即可</p><h1 id="自增长主键id" tabindex="-1">自增长主键ID <a class="header-anchor" href="#自增长主键id" aria-label="Permalink to &quot;自增长主键ID&quot;">​</a></h1><h2 id="自增长特点" tabindex="-1">自增长特点 <a class="header-anchor" href="#自增长特点" aria-label="Permalink to &quot;自增长特点&quot;">​</a></h2><blockquote><p>1、标识列必须和主键搭配吗？不一定，<strong>但要求是一个key</strong></p><p>2、一个表可以有几个标识列？<strong>至多一个！</strong></p><p>3、标识列的<strong>类型只能是数值型</strong></p><p>4、标识列可以通过 SET auto_increment_increment=3; 设置步长.可以通过 手动插入值，设置起始值</p></blockquote><p>在实际使用过程中，推荐使用主键自增ID和雪花算法生成的随机ID。</p><p>但是使用自增ID也有缺点：</p><blockquote><p>1、别人一旦爬取你的数据库，就可以根据数据库的自增id获取到你的业务增长信息，很容易进行数据窃取。2、其次，对于高并发的负载，innodb在按主键进行插入的时候会造成明显的锁争用，主键的上界会成为争抢的热点，因为所有的插入都发生在这里，并发插入会导致间隙锁竞争。</p></blockquote><p>总结起来，如果业务量小，推荐采用自增ID，如果业务量大，推荐采用雪花算法生成的随机ID。</p><h2 id="自增长id使用" tabindex="-1">自增长ID使用 <a class="header-anchor" href="#自增长id使用" aria-label="Permalink to &quot;自增长ID使用&quot;">​</a></h2><p>创建表</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">wan</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;">   id </span><span style="color:#C792EA;">INT</span><span style="color:#A6ACCD;"> auto_increment </span><span style="color:#C792EA;">primary key</span><span style="color:#A6ACCD;"> ,</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#F78C6C;">NAME</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">25</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">UNIQUE</span><span style="color:#A6ACCD;"> ,</span></span>
<span class="line"><span style="color:#A6ACCD;">   seat </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">25</span><span style="color:#A6ACCD;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span></code></pre></div><p>插入数据</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 进行插入数据，自动递增的那一列变成null即可</span></span>
<span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> wan </span><span style="color:#F78C6C;">VALUES</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张三</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1234</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> wan </span><span style="color:#F78C6C;">VALUES</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">李四</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">4321</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><p>设置和获取步长(可选)</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 展示增长步长</span></span>
<span class="line"><span style="color:#A6ACCD;">SHOW VARIABLES </span><span style="color:#F78C6C;">LIKE</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%auto_increment%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 增长步长</span></span>
<span class="line"><span style="color:#F78C6C;">SET</span><span style="color:#A6ACCD;"> auto_increment_increment</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p>再次插入数据</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> wan </span><span style="color:#F78C6C;">VALUES</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">李五</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">0987</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> wan </span><span style="color:#F78C6C;">VALUES</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">李六</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">10987</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><p>进行查看数据</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> wan;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220120213446430.png" alt="image-20220120213446430" style="zoom:80%;"><h2 id="自增id用完了会怎么样" tabindex="-1">自增ID用完了会怎么样 <a class="header-anchor" href="#自增id用完了会怎么样" aria-label="Permalink to &quot;自增ID用完了会怎么样&quot;">​</a></h2><p>自增ID达到上限用完了之后，分为两种情况：</p><blockquote><ol><li>如果设置了主键，那么将会报错主键冲突。</li><li>如果没有设置主键，数据库则会帮我们自动生成一个全局的row_id，新数据会覆盖老数据</li></ol></blockquote><p>解决方案：</p><blockquote><p>表尽可能都要设置主键，主键尽量使用bigint类型，21亿的上限还是有可能达到的，比如魔兽，虽然说row_id上限高达281万亿，但是覆盖数据显然是不可接受的。</p></blockquote><h2 id="主键一定要自增吗" tabindex="-1">主键一定要自增吗 <a class="header-anchor" href="#主键一定要自增吗" aria-label="Permalink to &quot;主键一定要自增吗&quot;">​</a></h2><blockquote><ul><li>建表SQL里主键边上的<code>AUTO_INCREMENT</code>，可以让主键自增，去掉它是可以的，但这就需要你在insert的时候自己设置主键的值。</li><li>建表sql里的 <code>PRIMARY KEY</code> 是用来声明主键的，如果去掉，那也能建表成功，但mysql内部会给你偷偷建一个 <code>ROW_ID</code>的隐藏列作为主键。</li><li>由于mysql使用<strong>B+树索引，叶子节点是从小到大排序的</strong>，如果使用自增id做主键，这样每次数据都加在B+树的最后，比起每次加在B+树中间的方式，加在最后可以有效<strong>减少页分裂的问题。</strong></li><li>在分库分表的场景下，我们可以通过redis等第三方组件来获得严格自增的主键id。如果不想依赖redis，可以参考雪花算法进行<strong>魔改</strong>，<strong>既能保证数据趋势递增，也能很好的满足分库分表的动态扩容。</strong></li><li>并不是所有数据库都建议使用自增id作为主键，比如<strong>tidb就推荐使用随机id</strong>，这样可以有效避免<strong>写热点</strong>的问题。而对于一些敏感数据，比如用户id，订单id等，如果使用自增id作为主键的话，外部通过抓包，很容易可以知道新进用户量，成单量这些信息，所以需要<strong>谨慎考虑</strong>是否继续使用自增主键。</li></ul></blockquote><h2 id="主键自增的问题" tabindex="-1">主键自增的问题 <a class="header-anchor" href="#主键自增的问题" aria-label="Permalink to &quot;主键自增的问题&quot;">​</a></h2><p><a href="https://mp.weixin.qq.com/s?__biz=MzAxNTM4NzAyNg==&amp;mid=2247503717&amp;idx=1&amp;sn=99ce327b3f18bab89bbef84f53d65a32&amp;chksm=9b865f98acf1d68effe3313633ddc9bbdae837eec7e0b128f7feb8842448aa8f99e1d5cd2d6c&amp;mpshare=1&amp;scene=23&amp;srcid=0420Kl1eSJOhI3VL8GinRWAN&amp;sharer_sharetime=1682002901202&amp;sharer_shareid=29b8a04db1dbd975e3bf4e9f47e7ac67#rd" target="_blank" rel="noreferrer">MySQL 主键自增，那些奇奇怪怪的坑</a></p><p>一般来说，主键自增是没有什么问题的。但是，如果在高并发环境下，就会有问题了。首先最容易想到的就是在高并发插入的时候产生的尾部热点问题，并发插入时，大家都需要去查询这个值然后计算出自己的主键值，那么主键的上界就会成为热点数据，并发插入时这里会产生锁竞争。为了解决这个问题，我们就需要选择适合自己的 <code>innodb_autoinc_lock_mode</code>。</p><h1 id="库和表占用空间大小⭐" tabindex="-1">库和表占用空间大小⭐ <a class="header-anchor" href="#库和表占用空间大小⭐" aria-label="Permalink to &quot;库和表占用空间大小⭐&quot;">​</a></h1><blockquote><p>在MySQL中有一个默认的数据表information_schema，information_schema这张数据表保存了MySQL服务器所有数据库的信息。如数据库名，数据库的表，表栏的数据类型与访问权限等。</p></blockquote><blockquote><p>再简单点，这台MySQL服务器上，到底有哪些数据库、各个数据库有哪些表，每张表的字段类型是什么，各个数据库要什么权限才能访问，等等信息都保存在information_schema表里面，所以请勿删改此表。</p></blockquote><h2 id="所有数据库容量大小" tabindex="-1">所有数据库容量大小 <a class="header-anchor" href="#所有数据库容量大小" aria-label="Permalink to &quot;所有数据库容量大小&quot;">​</a></h2><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span></span>
<span class="line"><span style="color:#A6ACCD;">table_schema </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">数据库</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#82AAFF;">sum</span><span style="color:#A6ACCD;">(table_rows) </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">记录数</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#82AAFF;">sum</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">truncate</span><span style="color:#A6ACCD;">(data_length</span><span style="color:#89DDFF;">/</span><span style="color:#F78C6C;">1024</span><span style="color:#89DDFF;">/</span><span style="color:#F78C6C;">1024</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">)) </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">数据容量(MB)</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#82AAFF;">sum</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">truncate</span><span style="color:#A6ACCD;">(index_length</span><span style="color:#89DDFF;">/</span><span style="color:#F78C6C;">1024</span><span style="color:#89DDFF;">/</span><span style="color:#F78C6C;">1024</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">)) </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">索引容量(MB)</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> information_schema.tables</span></span>
<span class="line"><span style="color:#F78C6C;">group by</span><span style="color:#A6ACCD;"> table_schema</span></span>
<span class="line"><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">sum</span><span style="color:#A6ACCD;">(data_length) </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;">, </span><span style="color:#82AAFF;">sum</span><span style="color:#A6ACCD;">(index_length) </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209200904901.png" alt="image-20220920090424758" style="zoom:80%;"><h2 id="所有数据库各表容量大小" tabindex="-1">所有数据库各表容量大小 <a class="header-anchor" href="#所有数据库各表容量大小" aria-label="Permalink to &quot;所有数据库各表容量大小&quot;">​</a></h2><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span></span>
<span class="line"><span style="color:#A6ACCD;">table_schema </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">数据库</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">table_name </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">表名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">table_rows </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">记录数</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#F78C6C;">truncate</span><span style="color:#A6ACCD;">(data_length</span><span style="color:#89DDFF;">/</span><span style="color:#F78C6C;">1024</span><span style="color:#89DDFF;">/</span><span style="color:#F78C6C;">1024</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">数据容量(MB)</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#F78C6C;">truncate</span><span style="color:#A6ACCD;">(index_length</span><span style="color:#89DDFF;">/</span><span style="color:#F78C6C;">1024</span><span style="color:#89DDFF;">/</span><span style="color:#F78C6C;">1024</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">索引容量(MB)</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> information_schema.tables</span></span>
<span class="line"><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> data_length </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;">, index_length </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209202013163.png" alt="image-20220920201356933" style="zoom:80%;"><h2 id="指定数据库容量大小" tabindex="-1">指定数据库容量大小 <a class="header-anchor" href="#指定数据库容量大小" aria-label="Permalink to &quot;指定数据库容量大小&quot;">​</a></h2><blockquote><p>例：查看mysql库容量大小</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span></span>
<span class="line"><span style="color:#A6ACCD;">table_schema </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">数据库</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#82AAFF;">sum</span><span style="color:#A6ACCD;">(table_rows) </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">记录数</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#82AAFF;">sum</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">truncate</span><span style="color:#A6ACCD;">(data_length</span><span style="color:#89DDFF;">/</span><span style="color:#F78C6C;">1024</span><span style="color:#89DDFF;">/</span><span style="color:#F78C6C;">1024</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">)) </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">数据容量(MB)</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#82AAFF;">sum</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">truncate</span><span style="color:#A6ACCD;">(index_length</span><span style="color:#89DDFF;">/</span><span style="color:#F78C6C;">1024</span><span style="color:#89DDFF;">/</span><span style="color:#F78C6C;">1024</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">)) </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">索引容量(MB)</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> information_schema.tables</span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> table_schema</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">mysql</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209202013374.png" alt="image-20220920201330159" style="zoom:80%;"><h2 id="指定数据库各表容量大小" tabindex="-1">指定数据库各表容量大小 <a class="header-anchor" href="#指定数据库各表容量大小" aria-label="Permalink to &quot;指定数据库各表容量大小&quot;">​</a></h2><p>例：查看mysql库各表容量大小</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span></span>
<span class="line"><span style="color:#A6ACCD;">table_schema </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">数据库</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">table_name </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">表名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">table_rows </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">记录数</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#F78C6C;">truncate</span><span style="color:#A6ACCD;">(data_length</span><span style="color:#89DDFF;">/</span><span style="color:#F78C6C;">1024</span><span style="color:#89DDFF;">/</span><span style="color:#F78C6C;">1024</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">数据容量(MB)</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#F78C6C;">truncate</span><span style="color:#A6ACCD;">(index_length</span><span style="color:#89DDFF;">/</span><span style="color:#F78C6C;">1024</span><span style="color:#89DDFF;">/</span><span style="color:#F78C6C;">1024</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">索引容量(MB)</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> information_schema.tables</span></span>
<span class="line"><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> table_schema</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">mysql</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> data_length </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;">, index_length </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209202012735.png" alt="image-20220920201252507" style="zoom:80%;"><h1 id="函数⭐⭐" tabindex="-1">函数⭐⭐ <a class="header-anchor" href="#函数⭐⭐" aria-label="Permalink to &quot;函数⭐⭐&quot;">​</a></h1><p>函数 是指一段可以直接被另一段程序调用的程序或代码。 也就意味着，这一段程序或代码在MySQL中已经给我们提供</p><p>了，我们要做的就是在合适的业务场景调用对应的函数完成对应的业务需求即可。 那么，函数到底在哪儿使用呢？</p><p>我们先来看两个场景：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206171725052.png" alt="image-20220617172504938" style="zoom:67%;"><p>1). 在企业的OA或其他的人力系统中，经常会提供的有这样一个功能，每一个员工登录上来之后都能够看到当前员工入职的天数。 而在数据库中，存储的都是入职日期，如 2000-11-12，<code>那如果快速计算出天数呢？</code></p><p>2). 在做报表这类的业务需求中,我们要展示出学员的分数等级分布。而在数据库中，存储的是学生的分数值，如98/75，<code>如何快速判定分数的等级呢？</code></p><p>其实，上述的这一类的需求呢，我们通过MySQL中的函数都可以很方便的实现 。</p><p>MySQL中的函数主要分为以下四类： <code>字符串函数、数值函数、日期函数、流程函数。</code></p><h2 id="字符串函数" tabindex="-1">字符串函数 <a class="header-anchor" href="#字符串函数" aria-label="Permalink to &quot;字符串函数&quot;">​</a></h2><p>MySQL中内置了很多字符串函数，常用的几个如下：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209192243070.png" alt="image-20220919224345956" style="zoom:80%;"><h3 id="concat-字符串拼接" tabindex="-1">concat : 字符串拼接 <a class="header-anchor" href="#concat-字符串拼接" aria-label="Permalink to &quot;concat : 字符串拼接&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">concat</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Hello</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> , </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;"> MySQL</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">); </span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">concat</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">version</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">_</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">database</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">_</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,user</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">#concat 拼接字符串</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">concat</span><span style="color:#A6ACCD;">(last_name,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">_</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,first_name) 姓名 </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> employees;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">#将姓变大写，名变小写，然后拼接</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">concat</span><span style="color:#A6ACCD;">(</span><span style="color:#82AAFF;">UPPER</span><span style="color:#A6ACCD;">(last_name),</span><span style="color:#82AAFF;">LOWER</span><span style="color:#A6ACCD;">(first_name))  姓名 </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> employees;</span></span></code></pre></div><h3 id="lower-全部转小写" tabindex="-1">lower : 全部转小写 <a class="header-anchor" href="#lower-全部转小写" aria-label="Permalink to &quot;lower : 全部转小写&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">lower</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Hello</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="upper-全部转大写" tabindex="-1">upper : 全部转大写 <a class="header-anchor" href="#upper-全部转大写" aria-label="Permalink to &quot;upper : 全部转大写&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">upper</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Hello</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="lpad-左填充" tabindex="-1">lpad : 左填充 <a class="header-anchor" href="#lpad-左填充" aria-label="Permalink to &quot;lpad : 左填充&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- ---01</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> lpad(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">01</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">-</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><blockquote><p>由于业务需求变更，企业员工的工号，统一为5位数，目前不足5位数的全部在前面补0</p><p>比如： 1号员工的工号应该为00001，注意：字段类型必须为字符串类型，为int类型是无法补0的</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206171732340.png" alt="image-20220617173243217" style="zoom:67%;"><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> workno </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> lpad(workno, </span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">0</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><p>处理完毕后, 具体的数据为:</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206171733164.png" alt="image-20220617173331049" style="zoom:67%;"><h3 id="rpad-右填充" tabindex="-1">rpad : 右填充 <a class="header-anchor" href="#rpad-右填充" aria-label="Permalink to &quot;rpad : 右填充&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 01---</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> rpad(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">01</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">-</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="trim-去除空格" tabindex="-1">trim : 去除空格 <a class="header-anchor" href="#trim-去除空格" aria-label="Permalink to &quot;trim : 去除空格&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">trim</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;"> Hello MySQL </span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="substring-substr-截取子字符串" tabindex="-1">substring ,substr: 截取子字符串 <a class="header-anchor" href="#substring-substr-截取子字符串" aria-label="Permalink to &quot;substring ,substr: 截取子字符串&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 注意，索引从1开始，Hello</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">substring</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Hello MySQL</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">.substr、substring</span></span>
<span class="line"><span style="color:#A6ACCD;"># 注意：索引从1开始</span></span>
<span class="line"><span style="color:#A6ACCD;">#截取从指定索引处后面所有字符,陆展元</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> substr(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">李莫愁爱上了陆展元</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">7</span><span style="color:#A6ACCD;">)  out_put;</span></span>
<span class="line"><span style="color:#A6ACCD;">#截取从指定索引处指定字符长度的字符,李莫愁</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> substr(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">李莫愁爱上了陆展元</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">) out_put;</span></span></code></pre></div><h3 id="length-获取字节个数" tabindex="-1">length 获取字节个数 <a class="header-anchor" href="#length-获取字节个数" aria-label="Permalink to &quot;length 获取字节个数&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#一注意：一个汉字是3个字节，如第二个length长度为15</span></span>
<span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">.</span><span style="color:#F78C6C;">length</span><span style="color:#A6ACCD;"> 获取参数值的字节个数</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">length</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">john</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">length</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张三丰hahaha</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="char-length获取字符个数" tabindex="-1">char_length获取字符个数 <a class="header-anchor" href="#char-length获取字符个数" aria-label="Permalink to &quot;char_length获取字符个数&quot;">​</a></h3><p>返回值为字符串str所包含的字符个数。一个多字节字符算作一个单字符</p><p>有时候我们需要获取字符的<code>长度</code>，然后根据字符的长度进行<code>排序</code>。MYSQL给我们提供了一些有用的函数，比如：<code>char_length</code>。通过该函数就能获取字符长度。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> CHAR_LENGTH(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">任硕</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">); </span><span style="color:#676E95;font-style:italic;">-- 2</span></span></code></pre></div><p>获取字符长度并且排序的sql如下：</p><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">select * from brand where name like &#39;%苏三%&#39; </span></span>
<span class="line"><span style="color:#A6ACCD;">order by char_length(name) asc limit 5;</span></span></code></pre></div><p>执行效果如图所示：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202212201738543.png" alt="image-20221220173846413" style="zoom:50%;"><p>name字段使用关键字<code>模糊查询</code>之后，再使用<code>char_length</code>函数获取name字段的字符长度，然后按长度<code>升序</code>。</p><h3 id="instr返回子串第一次出现的索引" tabindex="-1">instr返回子串第一次出现的索引 <a class="header-anchor" href="#instr返回子串第一次出现的索引" aria-label="Permalink to &quot;instr返回子串第一次出现的索引&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">.instr 返回子串第一次出现的索引，如果找不到返回0，下面的案例返回结果为3，索引从0开始</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> INSTR(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">杨不殷六侠悔爱上了殷六侠</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">殷六</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">AS</span><span style="color:#A6ACCD;"> out_put;</span></span></code></pre></div><h3 id="replace-替换" tabindex="-1">replace 替换 <a class="header-anchor" href="#replace-替换" aria-label="Permalink to &quot;replace 替换&quot;">​</a></h3><p>我们经常会有替换字符串中部分内容的需求，比如：将字符串中的字符A替换成B。这种情况就能使用<code>replace</code>函数</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> brand </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#89DDFF;">=</span><span style="color:#82AAFF;">REPLACE</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">A</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">B</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> id</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p>这样就能轻松实现字符替换功能。也能用该函数去掉<code>前后空格</code>：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> brand </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#89DDFF;">=</span><span style="color:#82AAFF;">REPLACE</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;&#39;</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">like</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;"> %</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> brand </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#89DDFF;">=</span><span style="color:#82AAFF;">REPLACE</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;&#39;</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">like</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">% </span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p>使用该函数还能替换<code>json格式</code>的数据内容，真的非常有用。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># </span><span style="color:#F78C6C;">9</span><span style="color:#A6ACCD;">.</span><span style="color:#F78C6C;">replace</span><span style="color:#A6ACCD;"> 替换</span></span>
<span class="line"><span style="color:#A6ACCD;"># 赵敏赵敏张无忌爱上赵敏</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">REPLACE</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">周芷若周芷若张无忌爱上周芷若</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">周芷若</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">赵敏</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">AS</span><span style="color:#A6ACCD;"> out_put;</span></span></code></pre></div><h3 id="group-concat-字段拼接" tabindex="-1">group_concat 字段拼接 <a class="header-anchor" href="#group-concat-字段拼接" aria-label="Permalink to &quot;group_concat 字段拼接&quot;">​</a></h3><p>在我们平常的工作中，使用<code>group by</code>进行分组的场景，是非常多的。</p><p>比如想统计出用户表中，名称不同的用户的具体名称有哪些？</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> user </span><span style="color:#F78C6C;">group by</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p>但如果想把name相同的code拼接在一起，放到另外一列中该怎么办呢？答：使用<code>group_concat</code>函数。例</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">,group_concat(code) </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">user</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">group by</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p><img src="https://mmbiz.qpic.cn/mmbiz_png/ibJZVicC7nz5jCDA5gqGg5txINBrQaWepDb0YYOF5FtibiaHBtYn7EbPjVWIcH9WpAiapRVmFjGptb9icic3sPm420x8A/640?wx_fmt=png&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" alt="图片"></p><p>使用<code>group_concat</code>函数，可以轻松的把分组后，name相同的数据拼接到一起，组成一个字符串，用<code>逗号</code>分隔。</p><h2 id="数学函数" tabindex="-1">数学函数 <a class="header-anchor" href="#数学函数" aria-label="Permalink to &quot;数学函数&quot;">​</a></h2><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209192249084.png" alt="image-20220919224914971" style="zoom:80%;"><h3 id="ceil-向上取整" tabindex="-1">ceil：向上取整 <a class="header-anchor" href="#ceil-向上取整" aria-label="Permalink to &quot;ceil：向上取整&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#ceil 向上取整,返回</span><span style="color:#89DDFF;">&gt;=</span><span style="color:#A6ACCD;">该参数的最小整数，结果</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">1</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> CEIL(</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">.</span><span style="color:#F78C6C;">02</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="floor-向下取整" tabindex="-1">floor：向下取整 <a class="header-anchor" href="#floor-向下取整" aria-label="Permalink to &quot;floor：向下取整&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#floor 向下取整，返回</span><span style="color:#89DDFF;">&lt;=</span><span style="color:#A6ACCD;">该参数的最大整数，结果</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">10</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">FLOOR</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">9</span><span style="color:#A6ACCD;">.</span><span style="color:#F78C6C;">99</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="mod-取模" tabindex="-1">mod：取模 <a class="header-anchor" href="#mod-取模" aria-label="Permalink to &quot;mod：取模&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#mod取余，两个结果都为1</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> MOD(</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">%</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h3 id="rand-获取随机数" tabindex="-1">rand：获取随机数 <a class="header-anchor" href="#rand-获取随机数" aria-label="Permalink to &quot;rand：获取随机数&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 随机数：0-1之间，0.6280307812877286</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">rand</span><span style="color:#A6ACCD;">();</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 生成六位数验证码：177571，lpad作用是不足六位补0</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> lpad(</span><span style="color:#82AAFF;">round</span><span style="color:#A6ACCD;">(</span><span style="color:#82AAFF;">rand</span><span style="color:#A6ACCD;">()</span><span style="color:#89DDFF;">*</span><span style="color:#F78C6C;">1000000</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">),</span><span style="color:#F78C6C;">6</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="round-四舍五入" tabindex="-1">round：四舍五入 <a class="header-anchor" href="#round-四舍五入" aria-label="Permalink to &quot;round：四舍五入&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#round 四舍五入</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">ROUND</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">.</span><span style="color:#F78C6C;">55</span><span style="color:#A6ACCD;">);  # 结果为</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">2</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">ROUND</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">.</span><span style="color:#F78C6C;">567</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">);  # 保留两位小数，结果为1.57</span></span></code></pre></div><h3 id="truncate-截断" tabindex="-1">truncate 截断 <a class="header-anchor" href="#truncate-截断" aria-label="Permalink to &quot;truncate 截断&quot;">​</a></h3><p>就是保留小数点后面几位，留几位就写几位</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">truncate</span><span style="color:#A6ACCD;"> 截断，结果1.6</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TRUNCATE</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">.</span><span style="color:#F78C6C;">69999</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="format-格式化" tabindex="-1">format 格式化 <a class="header-anchor" href="#format-格式化" aria-label="Permalink to &quot;format 格式化&quot;">​</a></h3><p><code>format(x,n)</code>将数字<code>x</code>进行格式化，并以四舍五入的方式保留小数点后<code>n</code>位，结果以字符串的形式返回。</p><p>如果<code>n</code>为0，则返回结果函数不含小数部分。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">format</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">999</span><span style="color:#A6ACCD;">.</span><span style="color:#F78C6C;">12345678</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">), </span><span style="color:#82AAFF;">format</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">888</span><span style="color:#A6ACCD;">.</span><span style="color:#F78C6C;">12345678</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202212191025660.png" alt="image-20221219102506575" style="zoom:67%;"><h3 id="conv-进制转换" tabindex="-1">conv：进制转换 <a class="header-anchor" href="#conv-进制转换" aria-label="Permalink to &quot;conv：进制转换&quot;">​</a></h3><p><code>conv(n, from_base, to_base)</code>这个函数可以进行不同进制数之间的相互转换。比如将16进制的数字转换成2进制，亦或者将2进制的数字转换成10进制都可以。</p><p>返回值为数值<code>n</code>的字符串表示，由<code>from_base</code>进制转化为<code>to_base</code>进制。如果任意一个参数为<code>null</code>的话，那么返回值就是<code>null</code>。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> conv(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">a</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">16</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">16 to 10</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span></span>
<span class="line"><span style="color:#A6ACCD;">       conv(</span><span style="color:#F78C6C;">123</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">)  </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">10 to 2</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span></span>
<span class="line"><span style="color:#A6ACCD;">       conv(</span><span style="color:#F78C6C;">123</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">16</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">10 to 16</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span></span>
<span class="line"><span style="color:#A6ACCD;">       conv(</span><span style="color:#F78C6C;">123</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">8</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">10 to 8</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302261724091.png" alt="image-20230226172418998" style="zoom:80%;"><h3 id="inet-auto-ip地址和数字转换⭐" tabindex="-1">inet_auto：IP地址和数字转换⭐ <a class="header-anchor" href="#inet-auto-ip地址和数字转换⭐" aria-label="Permalink to &quot;inet_auto：IP地址和数字转换⭐&quot;">​</a></h3><blockquote><p><code>inet_auto(expr)</code>给出一个作为字符串的网络地址的点地址表示，返回一个代表该地址数值的整数。地址可以是4或者8<code>bit</code>地址。</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> inet_aton(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">192.168.1.1</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> inet_aton(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">192.168.22.130</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><p>反之，我们也可以使用<code>inet_ntoa</code>函数将数值网络地址转换为字符串网络点地址，往下看：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> inet_ntoa(</span><span style="color:#F78C6C;">3232235777</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h2 id="日期函数⭐" tabindex="-1">日期函数⭐ <a class="header-anchor" href="#日期函数⭐" aria-label="Permalink to &quot;日期函数⭐&quot;">​</a></h2><p>常见的日期函数如下：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209192254080.png" alt="image-20220919225439961" style="zoom:80%;"><ol><li><code>NOW()</code>：返回当前日期和时间。</li><li><code>CURDATE()</code>：返回当前日期。</li><li><code>CURTIME()</code>：返回当前时间。</li><li><code>DATE()</code>：提取日期或日期时间表达式的日期部分。</li><li><code>TIME()</code>：提取日期或日期时间表达式的时间部分。</li><li><code>YEAR()</code>：提取日期或日期时间表达式的年份部分。</li><li><code>MONTH()</code>：提取日期或日期时间表达式的月份部分。</li><li><code>DAY()</code>：提取日期或日期时间表达式的日部分。</li><li><code>HOUR()</code>：提取日期或日期时间表达式的小时部分。</li><li><code>MINUTE()</code>：提取日期或日期时间表达式的分钟部分。</li><li><code>SECOND()</code>：提取日期或日期时间表达式的秒部分。</li><li><code>DATEDIFF(date1,date2)</code>：计算两个日期之间的天数差。</li><li><code>DATE_ADD(date, INTERVAL value unit)</code>：将日期加上一定的时间间隔，返回一个新日期。</li><li><code>DATE_SUB(date, INTERVAL value unit)</code>：将日期减去一定的时间间隔，返回一个新日期。</li><li><code>DATE_FORMAT(date,format)</code>：将日期或日期时间格式化为指定的格式。</li></ol><h3 id="now-curdate-curtime" tabindex="-1">now，curdate，curtime <a class="header-anchor" href="#now-curdate-curtime" aria-label="Permalink to &quot;now，curdate，curtime&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">now</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">,curdate</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">, curtime</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">now</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302261935856.png" alt="image-20230226193557754" style="zoom:80%;"><h3 id="year-month-day" tabindex="-1">year , month , day <a class="header-anchor" href="#year-month-day" aria-label="Permalink to &quot;year , month , day&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">year</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2022-3-4</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">),</span><span style="color:#82AAFF;">month</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2025-3-12 12:22:33</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">),</span><span style="color:#82AAFF;">day</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">now</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302262052026.png" alt="image-20230226205236925" style="zoom:80%;"><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 通过截取字符串的方式获取年月</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">LEFT</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">now</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">) 年, MID(</span><span style="color:#F78C6C;">now</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">6</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">) 月;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209201838989.png" alt="image-20220920183802761" style="zoom:80%;"><h3 id="date-day-time" tabindex="-1">date，day，time <a class="header-anchor" href="#date-day-time" aria-label="Permalink to &quot;date，day，time&quot;">​</a></h3><ul><li>DATE() <code>提取日期或日期/时间表达式的日期部分</code></li><li>DAY() <code>返回当月的几号 (1-31)</code></li><li>TIME() <code>提取日期或日期/时间表达式的时间部分</code></li></ul><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">date</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2003-12-31 01:02:03</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">), </span><span style="color:#82AAFF;">day</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2017-02-03</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">), </span><span style="color:#C792EA;">time</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2003-12-31 01:02:03</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302262049938.png" alt="image-20230226204907836" style="zoom:80%;"><h3 id="dayofweek-hour" tabindex="-1">dayofweek hour <a class="header-anchor" href="#dayofweek-hour" aria-label="Permalink to &quot;dayofweek  hour&quot;">​</a></h3><blockquote><p>dayofweek：表示返回日期是星期几，记住：星期天=1，星期一=2， ... 星期六=7</p><p>hour：返回该date或者time的hour值，值范围（0-23).</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> DAYOFWEEK(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2007-02-03</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);    </span><span style="color:#89DDFF;">-&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">7</span></span>
<span class="line"><span style="color:#A6ACCD;">#查询星期六下的订单</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">order</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">WHERE</span><span style="color:#A6ACCD;"> DAYOFWEEK(OrderDate) </span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">7</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">HOUR</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">10:05:03</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);   </span><span style="color:#89DDFF;">-&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">10</span></span>
<span class="line"><span style="color:#A6ACCD;">#查看2019年双11，0点下单量</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">count</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">order</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">WHERE</span><span style="color:#A6ACCD;">  OrderDate</span></span>
<span class="line"><span style="color:#F78C6C;">BETWEEN</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2019-11-11</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2019-11-12</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">and</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">HOUR</span><span style="color:#A6ACCD;">(OrderDate) </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;"> ;</span></span></code></pre></div><h3 id="extract-提取年月日" tabindex="-1">extract 提取年月日 <a class="header-anchor" href="#extract-提取年月日" aria-label="Permalink to &quot;extract 提取年月日&quot;">​</a></h3><blockquote><p>用于返回日期/时间的单独部分，比如年、月、日、小时、分钟等等。</p></blockquote><p>date 参数是合法的日期表达式。unit 参数可以是下列的值：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202207181532956.png" alt="image-20220718153243873" style="zoom:50%;"><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> EXTRACT(</span><span style="color:#F78C6C;">YEAR</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2019-07-02</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);                 </span><span style="color:#89DDFF;">-&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2019</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> EXTRACT(YEAR_MONTH </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2019-07-02 01:02:03</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);  </span><span style="color:#89DDFF;">-&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">201907</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> EXTRACT(DAY_MINUTE </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2019-07-02 01:02:03</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);  </span><span style="color:#89DDFF;">-&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">20102</span></span></code></pre></div><p>查表示例</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> extract(</span><span style="color:#F78C6C;">year</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> createtime) </span><span style="color:#F78C6C;">AS</span><span style="color:#A6ACCD;"> 年,</span></span>
<span class="line"><span style="color:#A6ACCD;">extract(</span><span style="color:#F78C6C;">month</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> createtime) </span><span style="color:#F78C6C;">AS</span><span style="color:#A6ACCD;"> 月,</span></span>
<span class="line"><span style="color:#A6ACCD;">extract(</span><span style="color:#F78C6C;">day</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> createtime) </span><span style="color:#F78C6C;">AS</span><span style="color:#A6ACCD;"> 日</span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> tb_user1;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202207181547609.png" alt="image-20220718154731527" style="zoom:67%;"><h3 id="date-add-date-sub" tabindex="-1">date_add，date_sub <a class="header-anchor" href="#date-add-date-sub" aria-label="Permalink to &quot;date_add，date_sub&quot;">​</a></h3><p>DATE_ADD: 向日期添加指定的时间间隔。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 2092-09-20 08:32:38</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> date_add(</span><span style="color:#F78C6C;">now</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">, INTERVAL </span><span style="color:#F78C6C;">70</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">YEAR</span><span style="color:#A6ACCD;"> );</span></span></code></pre></div><p>DATE_SUB()：从日期减去指定的时间间隔。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- &#39;2017-05-01&#39;</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> DATE_SUB(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2018-05-01</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,INTERVAL </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">YEAR</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#A6ACCD;"># 如果你查最近七天内的订单，可以这样：</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">order</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> OrderDate</span><span style="color:#89DDFF;">&gt;=</span><span style="color:#A6ACCD;">DATE_SUB(</span><span style="color:#F78C6C;">NOW</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">,INTERVAL </span><span style="color:#F78C6C;">7</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">DAY</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="datediff-获取两个日期相差的天数" tabindex="-1">datediff：获取两个日期相差的天数 <a class="header-anchor" href="#datediff-获取两个日期相差的天数" aria-label="Permalink to &quot;datediff：获取两个日期相差的天数&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight has-diff"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 512</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">datediff</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">NOW</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2021-1-21</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">) 相隔天数;</span></span></code></pre></div><p>查询所有员工的入职天数，并根据入职天数倒序排序。</p><p>思路： 入职天数，就是通过当前日期 - 入职日期，所以需要使用datediff函数来完成。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight has-diff"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">, </span><span style="color:#82AAFF;">datediff</span><span style="color:#A6ACCD;">(curdate</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">, entrydate) </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">entrydays</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> emp </span></span>
<span class="line"><span style="color:#F78C6C;">order by</span><span style="color:#A6ACCD;"> entrydays </span><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight has-diff"><code><span class="line"><span style="color:#A6ACCD;"># 如果你查最近30天内的订单，可以这样：</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> order </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">datediff</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">NOW</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">,OrderDate) </span><span style="color:#89DDFF;">&lt;=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">30</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h3 id="str-to-date-字符转日期" tabindex="-1">str_to_date 字符转日期 <a class="header-anchor" href="#str-to-date-字符转日期" aria-label="Permalink to &quot;str_to_date 字符转日期&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#str_to_date 将字符通过指定的格式转换成日期，结果：</span><span style="color:#F78C6C;">1998</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">03</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">02</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> STR_TO_DATE(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1998-3-2</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%Y-%c-%d</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">AS</span><span style="color:#A6ACCD;"> out_put;</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#查询入职日期为1992</span><span style="color:#676E95;font-style:italic;">--4-3的员工信息</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> employees </span><span style="color:#F78C6C;">WHERE</span><span style="color:#A6ACCD;"> hiredate </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1992-4-3</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> employees </span><span style="color:#F78C6C;">WHERE</span><span style="color:#A6ACCD;"> hiredate </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> STR_TO_DATE(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">4-3 1992</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%c-%d %Y</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="date-format" tabindex="-1">date_format <a class="header-anchor" href="#date-format" aria-label="Permalink to &quot;date_format&quot;">​</a></h3><p><strong>定义：</strong> 用于以不同的格式显示日期/时间数据。</p><p>date为对应的日期，fromat为输出格式。format的格式如下：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202207181549286.png" alt="image-20220718154923175" style="zoom:50%;"><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">DATE_FORMAT</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2009-10-04 22:23:00</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%W %M %Y</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);  </span><span style="color:#89DDFF;">-&gt;</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Sunday October 2009</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">DATE_FORMAT</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2007-10-04 22:23:00</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%H:%i:%s</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);   </span><span style="color:#89DDFF;">-&gt;</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">22:23:00</span><span style="color:#89DDFF;">&#39;</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">date_format</span><span style="color:#A6ACCD;"> 将日期转换成字符，结果为：21年08月16日</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">DATE_FORMAT</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">NOW</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%y年%m月%d日</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">AS</span><span style="color:#A6ACCD;"> out_put;</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#案例：查询有奖金的员工名和入职日期(xx月</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">xx日 xx年)</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> last_name,</span><span style="color:#F78C6C;">DATE_FORMAT</span><span style="color:#A6ACCD;">(hiredate,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%m月/%d日 %y年</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">) 入职日期</span></span>
<span class="line"><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> employees</span></span>
<span class="line"><span style="color:#F78C6C;">WHERE</span><span style="color:#A6ACCD;"> commission_pct </span><span style="color:#F78C6C;">IS NOT NULL</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h3 id="时间戳转换" tabindex="-1">时间戳转换 <a class="header-anchor" href="#时间戳转换" aria-label="Permalink to &quot;时间戳转换&quot;">​</a></h3><p>利用 FROM_UNIXTIME() 和 UNIX_TIMESTAMP() 函数</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># </span><span style="color:#F78C6C;">1655879641</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> unix_timestamp(</span><span style="color:#F78C6C;">now</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#A6ACCD;"># </span><span style="color:#F78C6C;">2022</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">06</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">22</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">14</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">34</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">01</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> from_unixtime(</span><span style="color:#F78C6C;">1655879641</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> unix_timestamp(</span><span style="color:#F78C6C;">now</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">) </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">时间戳</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, from_unixtime(</span><span style="color:#F78C6C;">1655879641</span><span style="color:#A6ACCD;">) </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">时间</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302262051843.png" alt="image-20230226205142744" style="zoom:80%;"><h2 id="其他函数" tabindex="-1">其他函数 <a class="header-anchor" href="#其他函数" aria-label="Permalink to &quot;其他函数&quot;">​</a></h2><h3 id="version-database-user" tabindex="-1">version,database,user <a class="header-anchor" href="#version-database-user" aria-label="Permalink to &quot;version,database,user&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">version</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">;  # 查看当前数据库版本 </span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">database</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">; # 查看当前使用的数据库</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> user</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">; # 查看当前用户：</span><span style="color:#F78C6C;">root</span><span style="color:#A6ACCD;">@localhost</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">version</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">数据库版本</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> ,</span><span style="color:#F78C6C;">database</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">当前数据库</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> , user</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">当前用户</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202212191044413.png" alt="image-20221219104432321" style="zoom:67%;"><h3 id="加锁函数和解锁函数" tabindex="-1">加锁函数和解锁函数 <a class="header-anchor" href="#加锁函数和解锁函数" aria-label="Permalink to &quot;加锁函数和解锁函数&quot;">​</a></h3><ul><li>GET_LOCK(str,timeout)设法使用字符串str给定的名字得到一个锁，超时为timeout秒。</li><li>RELEASE_LOCK(str)解开被GET_LOCK()获取的，用字符串str所命名的锁。</li><li>IS_FREE_LOCK(str)检查名为str的锁是否可以使用</li><li>IS_USED_LOCK(str)检查名为str的锁是否正在被使用</li></ul><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> get_lock(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">yunweijia</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">15</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> getlock,</span></span>
<span class="line"><span style="color:#A6ACCD;">       is_used_lock(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">yunweijia</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> isusedlock,</span></span>
<span class="line"><span style="color:#A6ACCD;">       is_free_lock(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">yunweijia</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> isfreelock,</span></span>
<span class="line"><span style="color:#A6ACCD;">       release_lock(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">yunweijia</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> releaselock;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202212191032999.png" alt="image-20221219103219911" style="zoom:67%;"><p><code>get_lock(&#39;yunweijia&#39;, 15)</code>：返回结果为1，说明成功得到了一个名称为‘yunweijia’的锁，持续时间为15秒；</p><p><code>is_used_lock(&#39;yunweijia&#39;)</code>：返回结果为当前连接<code>id</code>，表示名称为<code>yunweijia</code>的锁正在被使用；</p><p><code>is_free_lock(&#39;yunweijia&#39;)</code>：返回结果是0，说明名称为<code>yunweijia</code>的锁正在被使用；</p><p><code>release_lock(&#39;yunweijia&#39;)</code>：返回值为1，说明解锁成功了。</p><h3 id="测试函数效率" tabindex="-1">测试函数效率 <a class="header-anchor" href="#测试函数效率" aria-label="Permalink to &quot;测试函数效率&quot;">​</a></h3><blockquote><p>BENCHMARK(count,expr)函数重复count次执行表达式expr。他可以用于计算MYSQL处理表达式的速度。</p></blockquote><blockquote><p>结果值通常为0（0只是表示处理过程很快，并不是没有花费时间）另一个作用是他可以在MYSQL客户端内部报告语句执行的时间。</p></blockquote><blockquote><p>benchmark函数只有两个参数，第一个是执行次数，第二个是要测试的函数或者<a href="https://so.csdn.net/so/search?q=%E8%A1%A8%E8%BE%BE%E5%BC%8F&amp;spm=1001.2101.3001.7020" target="_blank" rel="noreferrer">表达式</a>。返回的结果始终是0，执行时间才是我们需要的结果</p></blockquote><p>下面使用BENCHMARK函数重复执行PASSWORD操作500000次</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> benchmark(</span><span style="color:#F78C6C;">50000000</span><span style="color:#A6ACCD;">, sha(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">123321sasd</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">));</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> benchmark(1e8,current_date</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> benchmark(1e8, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">select * from emp</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><blockquote><p>由此可以看出，使用BENCHMARK执行500000次的时间为0.49690秒，明显比执行一次的时间延长了。TIPS：BENCHMARK报告的时间是客户端经过的时间，而不是在服务器端的CPU时间，每次执行后报告的时间并不一定是相同的。请注意，这个查询语句可能会在数据库服务器上造成很大的负载，因为它会重复执行一条查询语句很多次。在生产环境中，应该非常谨慎地使用 <code>BENCHMARK()</code> 函数，以免对数据库服务器的性能产生负面影响。在测试和开发环境中使用时，应该仔细评估其影响，避免对其他用户和应用程序造成影响。</p></blockquote><h3 id="改变字符集的函数" tabindex="-1">改变字符集的函数 <a class="header-anchor" href="#改变字符集的函数" aria-label="Permalink to &quot;改变字符集的函数&quot;">​</a></h3><p>CONVERT(...using...)带有USING的CONVERT()函数被用来在不同的字符集之间转化数据。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> charset(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">yunweijia</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> coll,</span></span>
<span class="line"><span style="color:#A6ACCD;">       charset(</span><span style="color:#82AAFF;">convert</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">yunweijia</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">using</span><span style="color:#A6ACCD;"> latin1)) </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> coll_1;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202212191039126.png" alt="image-20221219103918046" style="zoom:80%;"><p>默认为utf8字符集，通过CONVERT()将字符串“string”的默认字符集改为latin1</p><h3 id="改变数据类型的函数" tabindex="-1">改变数据类型的函数 <a class="header-anchor" href="#改变数据类型的函数" aria-label="Permalink to &quot;改变数据类型的函数&quot;">​</a></h3><h4 id="基本使用" tabindex="-1">基本使用 <a class="header-anchor" href="#基本使用" aria-label="Permalink to &quot;基本使用&quot;">​</a></h4><p>CAST(x,AS type)和CONVERT(x,type)函数将一个类型的值转换为另一个类型的值，可转换的type有：</p><p>BINARY、CHAR(n)、DATE、TIME、DATETIME、DECIMAL、SIGNED、UNSIGNED</p><p>在SQLSERVER里也是使用这两个函数进行数据类型转换的~</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">cast</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">1000</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">char</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">)), </span><span style="color:#82AAFF;">convert</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2022-07-18 09:09:00</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#C792EA;">time</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202212191040470.png" alt="image-20221219104034387" style="zoom:67%;"><p>可以看到，<code>select cast(1000 as char(3))</code>将整数数据1000，转换为带有两个显示宽度的字符串类型，结果为100；<code>convert(&#39;2022-07-18 09:09:00&#39;, time)</code>将<code>datetime</code>类型的值转换为<code>time</code>类型值，结果是<code>09:09:00</code></p><h4 id="tips" tabindex="-1">TIPS <a class="header-anchor" href="#tips" aria-label="Permalink to &quot;TIPS&quot;">​</a></h4><p>1、MySQL中，日期时间以字符串形式存储在数据表中，因此可以使用字符串函数分别截取日期时间值的不同部分</p><p>2、修改默认的字符集，更改MySQL默认的字符集，在Windows中，只需要修改my.ini，该文件在MySQL安装目录下。</p><p>修改配置文件中的default-character-set和character-set-server参数值，将其改为想要的字符集名称，如：</p><p>gbk、gb2312、latin1等，修改完之后，重启MySQL服务，即可生效。</p><p>如果不确定当前使用的字符集，可以使用下面的SQL语句来查看当前字符集进行对比</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">SHOW VARIABLES </span><span style="color:#F78C6C;">LIKE</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">character_set_%</span><span style="color:#89DDFF;">&#39;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302261933129.png" alt="image-20230226193327002" style="zoom:80%;"><h2 id="流程函数if" tabindex="-1">流程函数if <a class="header-anchor" href="#流程函数if" aria-label="Permalink to &quot;流程函数if&quot;">​</a></h2><p>流程函数也是很常用的一类函数，可以在SQL语句中实现条件筛选，从而提高语句的效率。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209200835448.png" alt="image-20220920083529326" style="zoom:80%;"><h3 id="if" tabindex="-1">if <a class="header-anchor" href="#if" aria-label="Permalink to &quot;if&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">.if函数： </span><span style="color:#F78C6C;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">else</span><span style="color:#A6ACCD;"> 的效果</span></span>
<span class="line"><span style="color:#A6ACCD;"># 第一个参数是判断表达式，第二个参数为执行结果为true的情况，第三个参数为结果为false的情况</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">if</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">10</span><span style="color:#89DDFF;">&lt;</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">大</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">小</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="ifnull" tabindex="-1">ifnull <a class="header-anchor" href="#ifnull" aria-label="Permalink to &quot;ifnull&quot;">​</a></h3><p>如果为null，值为第二个</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">ifnull</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Ok</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Default</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">); </span></span>
<span class="line"></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">ifnull</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Default</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">); </span></span>
<span class="line"></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">ifnull</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Default</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="case-when-then-else-end" tabindex="-1">case when then else end <a class="header-anchor" href="#case-when-then-else-end" aria-label="Permalink to &quot;case when then else end&quot;">​</a></h3><h4 id="基本语法-10" tabindex="-1">基本语法 <a class="header-anchor" href="#基本语法-10" aria-label="Permalink to &quot;基本语法&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">case</span></span>
<span class="line"><span style="color:#F78C6C;">when</span><span style="color:#A6ACCD;"> 条件1 </span><span style="color:#F78C6C;">then</span><span style="color:#A6ACCD;"> 要显示的值1或语句1</span></span>
<span class="line"><span style="color:#F78C6C;">when</span><span style="color:#A6ACCD;"> 条件2 </span><span style="color:#F78C6C;">then</span><span style="color:#A6ACCD;"> 要显示的值2或语句2</span></span>
<span class="line"><span style="color:#F78C6C;">else</span><span style="color:#A6ACCD;"> 要显示的值n或语句n</span></span>
<span class="line"><span style="color:#F78C6C;">end</span></span></code></pre></div><h4 id="实战演练-6" tabindex="-1">实战演练 <a class="header-anchor" href="#实战演练-6" aria-label="Permalink to &quot;实战演练&quot;">​</a></h4><p>案例: 统计班级各个学员的成绩</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">score</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;">    id </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">ID</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">姓名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    math </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">数学</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    english </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">英语</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    chinese </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">语文</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">学员成绩表</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F78C6C;">insert into</span><span style="color:#A6ACCD;"> score(id, </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">, math, english, chinese)</span><span style="color:#F78C6C;">VALUES</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Tom</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">67</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">88</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">95</span><span style="color:#A6ACCD;"> ),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Rose</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> , </span><span style="color:#F78C6C;">23</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">66</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">90</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Jack</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">56</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">98</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">76</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span></span>
<span class="line"><span style="color:#A6ACCD;">    id,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    (</span><span style="color:#F78C6C;">case</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">when</span><span style="color:#A6ACCD;"> math </span><span style="color:#89DDFF;">&gt;=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">85</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">then</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">优秀</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">when</span><span style="color:#A6ACCD;"> math </span><span style="color:#89DDFF;">&gt;=</span><span style="color:#F78C6C;">60</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">then</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">及格</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">else</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">不及格</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">end</span><span style="color:#A6ACCD;"> ) </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">数学</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    (</span><span style="color:#F78C6C;">case</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">when</span><span style="color:#A6ACCD;"> english </span><span style="color:#89DDFF;">&gt;=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">85</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">then</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">优秀</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">when</span><span style="color:#A6ACCD;"> english </span><span style="color:#89DDFF;">&gt;=</span><span style="color:#F78C6C;">60</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">then</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">及格</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">else</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">不及格</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">end</span><span style="color:#A6ACCD;"> ) </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">英语</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    (</span><span style="color:#F78C6C;">case</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">when</span><span style="color:#A6ACCD;"> chinese </span><span style="color:#89DDFF;">&gt;=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">85</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">then</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">优秀</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">when</span><span style="color:#A6ACCD;"> chinese </span><span style="color:#89DDFF;">&gt;=</span><span style="color:#F78C6C;">60</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">then</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">及格</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">else</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">不及格</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">end</span><span style="color:#A6ACCD;"> ) </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">语文</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> score;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302261929135.png" alt="image-20230226192928022" style="zoom:80%;"><h1 id="常见约束" tabindex="-1">常见约束 <a class="header-anchor" href="#常见约束" aria-label="Permalink to &quot;常见约束&quot;">​</a></h1><h2 id="常见约束分析⭐" tabindex="-1">常见约束分析⭐ <a class="header-anchor" href="#常见约束分析⭐" aria-label="Permalink to &quot;常见约束分析⭐&quot;">​</a></h2><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220120115012574.png" alt="image-20220120115012574" style="zoom:80%;"><blockquote><p>注意：<strong>约束是作用于表中字段上的，可以在创建表/修改表的时候添加约束。</strong></p><p><strong>可以创建多个字段联合主键，在创建表时最后写上PRIMARY KEY(name, deptid)</strong></p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">表名</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;">   字段名 字段类型 列级约束,</span></span>
<span class="line"><span style="color:#A6ACCD;">   字段名 字段类型,</span></span>
<span class="line"><span style="color:#A6ACCD;">   表级约束</span></span>
<span class="line"><span style="color:#A6ACCD;">)</span></span></code></pre></div><h2 id="添加和删除约束" tabindex="-1">添加和删除约束 <a class="header-anchor" href="#添加和删除约束" aria-label="Permalink to &quot;添加和删除约束&quot;">​</a></h2><h3 id="创建表时添加约束" tabindex="-1">创建表时添加约束 <a class="header-anchor" href="#创建表时添加约束" aria-label="Permalink to &quot;创建表时添加约束&quot;">​</a></h3><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220120100344859.png" alt="image-20220120100344859" style="zoom:80%;"><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">user1</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;">    id  </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">primary key</span><span style="color:#A6ACCD;"> auto_increment comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">主键</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> ,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">50</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">not null</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">unique</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">姓名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    age </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">check</span><span style="color:#A6ACCD;">(age</span><span style="color:#89DDFF;">&gt;</span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;"> &amp;&amp; age</span><span style="color:#89DDFF;">&lt;</span><span style="color:#F78C6C;">120</span><span style="color:#A6ACCD;">) comment  </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">年龄</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F78C6C;">status</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">char</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">) </span><span style="color:#C792EA;">default</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">状态</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    gender </span><span style="color:#C792EA;">char</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">性别</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;">)comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">用户表</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p>进行插入数据测试</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">insert into</span><span style="color:#A6ACCD;"> user1(</span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">,age,</span><span style="color:#F78C6C;">status</span><span style="color:#A6ACCD;">,gender) </span><span style="color:#F78C6C;">values</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Tom</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">22</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">男</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">),(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Tom1</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">23</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">男</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><p>查看user1中的所有索引，包括主键、外键、唯一</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">show </span><span style="color:#F78C6C;">index</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> user1;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302270914432.png" alt="image-20230227091422328" style="zoom:80%;"><p>查看表结构</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">desc</span><span style="color:#A6ACCD;"> user1</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202212191111216.png" alt="image-20221219111127123" style="zoom:67%;"><h3 id="修改表时添加约束" tabindex="-1">修改表时添加约束 <a class="header-anchor" href="#修改表时添加约束" aria-label="Permalink to &quot;修改表时添加约束&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 1、添加列级约束</span></span>
<span class="line"><span style="color:#F78C6C;">alter</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> 表名 </span><span style="color:#F78C6C;">modify</span><span style="color:#A6ACCD;"> column 字段名 字段类型 新约束;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 2、添加表级约束</span></span>
<span class="line"><span style="color:#F78C6C;">alter</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> 表名 </span><span style="color:#F78C6C;">add</span><span style="color:#A6ACCD;"> 【</span><span style="color:#C792EA;">constraint</span><span style="color:#A6ACCD;"> 约束名】 约束类型(字段名) 【外键的引用】;</span></span></code></pre></div><p>案例</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">.添加非空约束</span></span>
<span class="line"><span style="color:#F78C6C;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> stuinfo </span><span style="color:#F78C6C;">MODIFY</span><span style="color:#A6ACCD;"> COLUMN stuname </span><span style="color:#C792EA;">VARCHAR</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;">)  </span><span style="color:#F78C6C;">NOT NULL</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">.添加默认约束</span></span>
<span class="line"><span style="color:#F78C6C;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> stuinfo </span><span style="color:#F78C6C;">MODIFY</span><span style="color:#A6ACCD;"> COLUMN age </span><span style="color:#C792EA;">INT</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">DEFAULT</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">18</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">.添加主键</span></span>
<span class="line"><span style="color:#A6ACCD;">#①列级约束</span></span>
<span class="line"><span style="color:#F78C6C;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> stuinfo </span><span style="color:#F78C6C;">MODIFY</span><span style="color:#A6ACCD;"> COLUMN id </span><span style="color:#C792EA;">INT</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">PRIMARY KEY</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">#②表级约束</span></span>
<span class="line"><span style="color:#F78C6C;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> stuinfo </span><span style="color:#F78C6C;">ADD</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">PRIMARY KEY</span><span style="color:#A6ACCD;">(id);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">#①列级约束</span></span>
<span class="line"><span style="color:#F78C6C;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> stuinfo </span><span style="color:#F78C6C;">MODIFY</span><span style="color:#A6ACCD;"> COLUMN seat </span><span style="color:#C792EA;">INT</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">UNIQUE</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">#②表级约束</span></span>
<span class="line"><span style="color:#F78C6C;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> stuinfo </span><span style="color:#F78C6C;">ADD</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">UNIQUE</span><span style="color:#A6ACCD;">(seat);</span></span></code></pre></div><h3 id="修改表时删除约束" tabindex="-1">修改表时删除约束 <a class="header-anchor" href="#修改表时删除约束" aria-label="Permalink to &quot;修改表时删除约束&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">.删除非空约束</span></span>
<span class="line"><span style="color:#F78C6C;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> stuinfo </span><span style="color:#F78C6C;">MODIFY</span><span style="color:#A6ACCD;"> COLUMN stuname </span><span style="color:#C792EA;">VARCHAR</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">NULL</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">.删除默认约束</span></span>
<span class="line"><span style="color:#F78C6C;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> stuinfo </span><span style="color:#F78C6C;">MODIFY</span><span style="color:#A6ACCD;"> COLUMN age </span><span style="color:#C792EA;">INT</span><span style="color:#A6ACCD;"> ;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">.删除主键</span></span>
<span class="line"><span style="color:#F78C6C;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> stuinfo </span><span style="color:#F78C6C;">DROP</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">PRIMARY KEY</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">.删除唯一</span></span>
<span class="line"><span style="color:#F78C6C;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> stuinfo </span><span style="color:#F78C6C;">DROP</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">INDEX</span><span style="color:#A6ACCD;"> seat;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">#</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">.删除外键</span></span>
<span class="line"><span style="color:#F78C6C;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> stuinfo </span><span style="color:#F78C6C;">DROP</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">FOREIGN KEY</span><span style="color:#A6ACCD;"> fk_stuinfo_major;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">SHOW </span><span style="color:#F78C6C;">INDEX</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> stuinfo;</span></span></code></pre></div><h2 id="外键约束-主从表一致性" tabindex="-1">外键约束(主从表一致性) <a class="header-anchor" href="#外键约束-主从表一致性" aria-label="Permalink to &quot;外键约束(主从表一致性)&quot;">​</a></h2><blockquote><p>外键用来在两个表的数据之间建立连接，可以是一列或者多列。一个表可以有一个或多个外键。外键对应的是参照完整性，<strong>一个表的外键可以是空值，若不为空值，则每一个外键值必须等于另一个表中主键的某个值</strong>。</p></blockquote><blockquote><p>外键：首先他是表中的一个字段，虽可以不是本表的主键，但要对应另外一个表的主键。外键的主要作用是保证数据引用的完整性，定义外键后，不允许删除在另一个表中具有关联关系的行。外键的作用是保持数据的一致性、完整性。</p></blockquote><blockquote><p>主表（父表）：对于两个具有关联关系的表而言，相关联字段中<strong>主键所在的那个表是主表</strong>。</p><p>从表（子表）：对于两个具有关联关系的表而言，相关联字段中<strong>外键所在的那个表是从表</strong>。</p></blockquote><h3 id="外键作用" tabindex="-1">外键作用 <a class="header-anchor" href="#外键作用" aria-label="Permalink to &quot;外键作用&quot;">​</a></h3><p>外键：用来让两张表的数据之间建立连接，从而保证数据的一致性和完整性。我们来看一个例子：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206171844850.png" alt="image-20220617184416724" style="zoom:67%;"><p>左侧的emp表是员工表，里面存储员工的基本信息，包含员工的ID、姓名、年龄、职位、薪资、入职日</p><p>期、上级主管ID、部门ID，在员工的信息中存储的是部门的ID dept_id，而这个部门的ID是关联的</p><p>部门表dept的主键id，那emp表的dept_id就是外键,关联的是另一张表的主键。</p><blockquote><p>注意：目前上述两张表，只是在逻辑上存在这样一层关系；在数据库层面，并未建立外键关联，所以是无法保证数据的一致性和完整性的。</p></blockquote><h3 id="外键语法" tabindex="-1">外键语法 <a class="header-anchor" href="#外键语法" aria-label="Permalink to &quot;外键语法&quot;">​</a></h3><h4 id="添加外键" tabindex="-1">添加外键 <a class="header-anchor" href="#添加外键" aria-label="Permalink to &quot;添加外键&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">alter</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> 表名 </span></span>
<span class="line"><span style="color:#F78C6C;">add</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">constraint</span><span style="color:#A6ACCD;"> 外键名称 </span><span style="color:#C792EA;">foreign key</span><span style="color:#A6ACCD;"> (外键字段) </span></span>
<span class="line"><span style="color:#C792EA;">references</span><span style="color:#A6ACCD;"> 主表名(主表字段名) </span></span>
<span class="line"><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> 行为 </span><span style="color:#C792EA;">on delete</span><span style="color:#A6ACCD;"> 行为;</span></span></code></pre></div><p>注意：当执行更新和删除操作时，上面的主表的变化对关联的一个表没影响，而反之则会产生影响</p><p>方式一：默认添加(不推荐)</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">alter</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> emp </span></span>
<span class="line"><span style="color:#F78C6C;">add</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">constraint</span><span style="color:#A6ACCD;"> fk_emp_dept_id </span></span>
<span class="line"><span style="color:#C792EA;">foreign key</span><span style="color:#A6ACCD;"> (dept_id) </span><span style="color:#C792EA;">references</span><span style="color:#A6ACCD;"> dept(id);</span></span></code></pre></div><p>方式二：设置外键约束cascade(更新删除行为)</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">alter</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> emp </span></span>
<span class="line"><span style="color:#F78C6C;">add</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">constraint</span><span style="color:#A6ACCD;"> fk_emp_dept_id </span><span style="color:#C792EA;">foreign key</span><span style="color:#A6ACCD;"> (dept_id) </span></span>
<span class="line"><span style="color:#C792EA;">references</span><span style="color:#A6ACCD;"> dept(id) </span></span>
<span class="line"><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> cascade </span><span style="color:#C792EA;">on delete cascade</span><span style="color:#A6ACCD;"> ;</span></span></code></pre></div><p>方式三：设置外键约束null</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">alter</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> emp </span></span>
<span class="line"><span style="color:#F78C6C;">add</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">constraint</span><span style="color:#A6ACCD;"> fk_emp_dept_id </span><span style="color:#C792EA;">foreign key</span><span style="color:#A6ACCD;"> (dept_id) </span></span>
<span class="line"><span style="color:#C792EA;">references</span><span style="color:#A6ACCD;"> dept(id) </span></span>
<span class="line"><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">on delete</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;"> ;</span></span></code></pre></div><h4 id="删除外键" tabindex="-1">删除外键 <a class="header-anchor" href="#删除外键" aria-label="Permalink to &quot;删除外键&quot;">​</a></h4><p>注意：如果本身有外键，再添加外键要先删除外键</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">alter</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">drop</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">foreign key</span><span style="color:#A6ACCD;"> 外键名;</span></span></code></pre></div><h3 id="删除-更新行为⭐" tabindex="-1">删除/更新行为⭐ <a class="header-anchor" href="#删除-更新行为⭐" aria-label="Permalink to &quot;删除/更新行为⭐&quot;">​</a></h3><p>添加了外键之后，再删除父表数据时产生的约束行为，我们就称为删除/更新行为。具体的删除/更新行为有以下几种</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220120105008855.png" alt="image-20220120105008855" style="zoom:80%;"><blockquote><ul><li>前面两个是默认行为，一旦执行更新和删除操作，就会报错</li><li>后面三个是自己设置的行为，很好用</li></ul></blockquote><h3 id="外键实战" tabindex="-1">外键实战 <a class="header-anchor" href="#外键实战" aria-label="Permalink to &quot;外键实战&quot;">​</a></h3><h4 id="创建表" tabindex="-1">创建表 <a class="header-anchor" href="#创建表" aria-label="Permalink to &quot;创建表&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 创建部门表</span></span>
<span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">dept</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;">    id   </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> auto_increment comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">ID</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">primary key</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">50</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">not null</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">部门名称</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;">)comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">部门表</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 插入数据</span></span>
<span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> dept (id, </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">) </span></span>
<span class="line"><span style="color:#F78C6C;">VALUES</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">研发部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">), </span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">市场部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">财务部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">), </span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">销售部</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">), </span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">总经办</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 创建员工表</span></span>
<span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">emp</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;">    id  </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> auto_increment comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">ID</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">primary key</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">50</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">not null</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">姓名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    age  </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">年龄</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    job </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">职位</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    salary </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">薪资</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    entrydate </span><span style="color:#C792EA;">date</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">入职时间</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    managerid </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">直属领导ID</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    dept_id </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">部门ID</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;">)comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">员工表</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 插入数据</span></span>
<span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> emp (id, </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">, age, job,salary, entrydate, managerid, dept_id) </span></span>
<span class="line"><span style="color:#F78C6C;">VALUES</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">金庸</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">66</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">总裁</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">20000</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2000-01-01</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张无忌</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">项目经理</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">12500</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2005-12-05</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">杨逍</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">33</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">开发</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">8400</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2000-11-03</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">韦一笑</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">48</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">开发</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">11000</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2002-02-05</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">常遇春</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">43</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">开发</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">10500</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2004-09-07</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">6</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">小昭</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">19</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">程序员鼓励师</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">6600</span><span style="color:#A6ACCD;">, </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2004-10-12</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><p>dept</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220120114610997.png" alt="image-20220120114610997" style="zoom:80%;"><p>emp</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220120114632516.png" alt="image-20220120114632516" style="zoom:80%;"><h4 id="添加外键-1" tabindex="-1">添加外键 <a class="header-anchor" href="#添加外键-1" aria-label="Permalink to &quot;添加外键&quot;">​</a></h4><p>方式一：默认添加(不推荐)</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">alter</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> emp </span></span>
<span class="line"><span style="color:#F78C6C;">add</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">constraint</span><span style="color:#A6ACCD;"> fk_emp_dept_id </span></span>
<span class="line"><span style="color:#C792EA;">foreign key</span><span style="color:#A6ACCD;"> (dept_id) </span><span style="color:#C792EA;">references</span><span style="color:#A6ACCD;"> dept(id);</span></span></code></pre></div><p>方式二：设置外键约束cascade</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">alter</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> emp </span></span>
<span class="line"><span style="color:#F78C6C;">add</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">constraint</span><span style="color:#A6ACCD;"> fk_emp_dept_id </span><span style="color:#C792EA;">foreign key</span><span style="color:#A6ACCD;"> (dept_id) </span></span>
<span class="line"><span style="color:#C792EA;">references</span><span style="color:#A6ACCD;"> dept(id) </span></span>
<span class="line"><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> cascade </span><span style="color:#C792EA;">on delete cascade</span><span style="color:#A6ACCD;"> ;</span></span></code></pre></div><p>修改dept表的id=1的字段</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> dept </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> id</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">7</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> id</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p>这时看emp表可以发现，id为1的全变成了7</p><p>emp表</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220120111631329.png" alt="image-20220120111631329" style="zoom:80%;"><blockquote><p>当然，删除也是对应删除的。相当于父表删除子表对应内容全删除</p></blockquote><p>方式三：设置外键约束null(推荐)</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">alter</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> emp </span></span>
<span class="line"><span style="color:#F78C6C;">add</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">constraint</span><span style="color:#A6ACCD;"> fk_emp_dept_id </span><span style="color:#C792EA;">foreign key</span><span style="color:#A6ACCD;"> (dept_id) </span></span>
<span class="line"><span style="color:#C792EA;">references</span><span style="color:#A6ACCD;"> dept(id) </span></span>
<span class="line"><span style="color:#F78C6C;">on</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">on delete</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;"> ;</span></span></code></pre></div><p>删除dept表id=5的字段</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">delete</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> dept </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> id </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">5</span></span></code></pre></div><p><strong>进入emp表进行查看，发现关联的外键被设置成了null</strong></p><h4 id="删除外键-1" tabindex="-1">删除外键 <a class="header-anchor" href="#删除外键-1" aria-label="Permalink to &quot;删除外键&quot;">​</a></h4><p>再次添加外键前要先删除外键</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">alter</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> emp </span><span style="color:#F78C6C;">drop</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">foreign key</span><span style="color:#A6ACCD;"> fk_emp_dept_id;</span></span></code></pre></div><h1 id="事务" tabindex="-1">事务 <a class="header-anchor" href="#事务" aria-label="Permalink to &quot;事务&quot;">​</a></h1><h2 id="概述" tabindex="-1">概述 <a class="header-anchor" href="#概述" aria-label="Permalink to &quot;概述&quot;">​</a></h2><blockquote><p><strong>事务是逻辑上的一组操作，要么都执行，要么都不执行,是一个不可分割的工作单位。</strong></p></blockquote><p>这个场景中我们需要插入多条相关联的数据到数据库，这个过程可能会遇到下面这些问题：</p><blockquote><ul><li>数据库中途突然因为某些原因挂掉了。</li><li>客户端突然因为网络原因连接不上数据库了。</li><li>并发访问数据库时，多个线程同时写入数据库，覆盖了彼此的更改。</li></ul></blockquote><blockquote><p>假如A转账给B 100 元，先从A的账户里扣除 100 元，再在 B 的账户上加上 100 元。如果扣完A的100元后，还没来得及给B加上，银行系统异常了，最后导致A的余额减少了，B的余额却没有增加。所以就需要事务，将A的钱回滚回去，就是这么简单。</p></blockquote><blockquote><p>为了解决上述的问题，就需要通过数据的事务来完成，我们只需要在业务逻辑执行之前开启事务，执行完毕后提交事务。如果执行过程中报错，则回滚事务，把数据恢复到事务开始之前的状态。</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206162224070.png" alt="image-20220616222423985" style="zoom:67%;"><blockquote><p>注意： <strong>默认MySQL的事务是自动提交的，也就是说，当执行完一条DML语句时，MySQL会立即隐式的提交事务</strong></p></blockquote><h2 id="基本语法-11" tabindex="-1">基本语法 <a class="header-anchor" href="#基本语法-11" aria-label="Permalink to &quot;基本语法&quot;">​</a></h2><h3 id="自动改手动" tabindex="-1">自动改手动 <a class="header-anchor" href="#自动改手动" aria-label="Permalink to &quot;自动改手动&quot;">​</a></h3><p>查看/设置事务提交方式</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> @@autocommit; </span><span style="color:#676E95;font-style:italic;">-- 查询事务提交方式：1表示自动，0表示手动</span></span>
<span class="line"><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> @@autocommit </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">; </span><span style="color:#676E95;font-style:italic;">-- 设置为手动提交</span></span>
<span class="line"><span style="color:#A6ACCD;"># 多条 </span><span style="color:#F78C6C;">SQL</span><span style="color:#A6ACCD;"> 语句</span></span>
<span class="line"><span style="color:#A6ACCD;">SQL1,SQL2,SQL3</span></span>
<span class="line"><span style="color:#F78C6C;">commit</span><span style="color:#A6ACCD;">；</span></span></code></pre></div><h3 id="推荐方式" tabindex="-1">推荐方式 <a class="header-anchor" href="#推荐方式" aria-label="Permalink to &quot;推荐方式&quot;">​</a></h3><p>开启事务</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">start transaction</span><span style="color:#A6ACCD;"> ;  或  </span><span style="color:#F78C6C;">begin</span><span style="color:#A6ACCD;"> ;</span></span></code></pre></div><p>提交事务</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">commit</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p>回滚事务</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 回滚事务</span></span>
<span class="line"><span style="color:#F78C6C;">rollback</span><span style="color:#A6ACCD;"> ;</span></span></code></pre></div><p>综合使用</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;"># 开启一个事务</span></span>
<span class="line"><span style="color:#F78C6C;">start transaction</span><span style="color:#A6ACCD;"> ;</span></span>
<span class="line"><span style="color:#A6ACCD;"># 多条 </span><span style="color:#F78C6C;">SQL</span><span style="color:#A6ACCD;"> 语句</span></span>
<span class="line"><span style="color:#A6ACCD;">SQL1,SQL2,SQL3</span></span>
<span class="line"><span style="color:#A6ACCD;"># 提交事务</span></span>
<span class="line"><span style="color:#F78C6C;">commit</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h2 id="四大特性和并发问题" tabindex="-1">四大特性和并发问题 <a class="header-anchor" href="#四大特性和并发问题" aria-label="Permalink to &quot;四大特性和并发问题&quot;">​</a></h2><h3 id="四大特性acid" tabindex="-1">四大特性ACID <a class="header-anchor" href="#四大特性acid" aria-label="Permalink to &quot;四大特性ACID&quot;">​</a></h3><blockquote><p>只有保证了事务的持久性、原子性、隔离性之后，一致性才能得到保障。也就是说 A、I、D 是手段，C 是目的！</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302271112570.png" alt="image-20230227111249423" style="zoom:80%;"><h3 id="并发问题" tabindex="-1">并发问题 <a class="header-anchor" href="#并发问题" aria-label="Permalink to &quot;并发问题&quot;">​</a></h3><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img/image-20220120193051267.png" alt="image-20220120193051267" style="zoom:80%;"><blockquote><p><strong>不可重复读和幻读区别</strong> ：不可重复读的重点是修改比如多次读取一条记录发现其中某些列的值被修改，幻读的重点在于新增或者删除比如多次查询同一条查询语句（DQL）时，记录发现记录增多或减少了</p></blockquote><h2 id="事务的隔离级别" tabindex="-1">事务的隔离级别 <a class="header-anchor" href="#事务的隔离级别" aria-label="Permalink to &quot;事务的隔离级别&quot;">​</a></h2><h3 id="四种隔离级别" tabindex="-1">四种隔离级别 <a class="header-anchor" href="#四种隔离级别" aria-label="Permalink to &quot;四种隔离级别&quot;">​</a></h3><blockquote><p><strong>READ-UNCOMMITTED(读取未提交)</strong> ： <strong>最低的隔离级别，允许读取尚未提交的数据变更，可能会导致脏读、幻读或不可重复读</strong>。</p></blockquote><blockquote><p><strong>READ-COMMITTED(读取已提交)</strong> ： <strong>允许读取并发事务已经提交的数据，可以阻止脏读，但是幻读或不可重复读仍有可能发生</strong>。</p></blockquote><blockquote><p><strong>REPEATABLE-READ(可重复读)</strong> ： <strong>对同一字段的多次读取结果都是一致的，除非数据是被本身事务自己所修改，可以阻止脏读和不可重复读，但幻读仍有可能发生</strong>。</p></blockquote><blockquote><p><strong>SERIALIZABLE(可串行化)</strong> ： <strong>最高的隔离级别，完全服从 ACID 的隔离级别。所有的事务依次逐个执行，这样事务之间就完全不可能产生干扰，也就是说，该级别可以防止脏读、不可重复读以及幻读</strong>。</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209201021387.png" alt="image-20220920102159181" style="zoom:80%;"><ul><li><p>mysql中默认 第三个隔离级别 repeatable read</p></li><li><p>oracle中默认第二个隔离级别 read committed</p></li></ul><h3 id="查看隔离级别" tabindex="-1">查看隔离级别 <a class="header-anchor" href="#查看隔离级别" aria-label="Permalink to &quot;查看隔离级别&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- MySQL8.0前</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> @@tx_isolation;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- MySQL 8.0 该命令改为</span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> @@transaction_isolation;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206161938075.png" alt="image-20220616193807000" style="zoom:80%;"><h3 id="设置隔离级别" tabindex="-1">设置隔离级别 <a class="header-anchor" href="#设置隔离级别" aria-label="Permalink to &quot;设置隔离级别&quot;">​</a></h3><p>通过以下 SQL 可以设置当前客户端的事务隔离级别：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">session</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">transaction</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">isolation</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">level</span><span style="color:#A6ACCD;"> 事务隔离级别;</span></span></code></pre></div><p>事务隔离级别的值有 4 个：READ UNCOMMITTED、READ COMMITTED、REPEATABLE READ、SERIALIZABLE。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">session</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">transaction</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">isolation</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">level</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">uncommitted</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">session</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">transaction</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">isolation</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">level</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">committed</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><blockquote><p>注意：<strong>事务隔离级别越高，数据越安全，但是性能越低</strong>。</p></blockquote><h2 id="四大隔离级别实践" tabindex="-1">四大隔离级别实践 <a class="header-anchor" href="#四大隔离级别实践" aria-label="Permalink to &quot;四大隔离级别实践&quot;">​</a></h2><p>既然并发事务存在<strong>脏读、不可重复、幻读</strong>等问题，InnoDB实现了哪几种事务的隔离级别应对呢？</p><ul><li>读未提交（Read Uncommitted）</li><li>读已提交（Read Committed）</li><li>可重复读（Repeatable Read）</li><li>串行化（Serializable）</li></ul><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> `</span><span style="color:#82AAFF;">account</span><span style="color:#A6ACCD;">`(</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">id</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">11</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">NOT NULL</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">name</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">255</span><span style="color:#A6ACCD;">) </span><span style="color:#C792EA;">DEFAULT</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">NULL</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">balance</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">11</span><span style="color:#A6ACCD;">) </span><span style="color:#C792EA;">DEFAULT</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">NULL</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">     </span><span style="color:#C792EA;">PRIMARY KEY</span><span style="color:#A6ACCD;"> (</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">id</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">     </span><span style="color:#F78C6C;">UNIQUE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">KEY</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">un_name_idx</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">name</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">)</span><span style="color:#F78C6C;">USING</span><span style="color:#A6ACCD;"> BTREE</span></span>
<span class="line"><span style="color:#A6ACCD;">)ENGINE</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">InnoDBDEFAULT CHARSET</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">utf8;</span></span>
<span class="line"><span style="color:#F78C6C;">insert into</span><span style="color:#A6ACCD;"> account(id, </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">, </span><span style="color:#C792EA;">money</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">VALUES</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张三</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2000</span><span style="color:#A6ACCD;">),(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">李四</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2000</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="读未提交-read-uncommitted" tabindex="-1">读未提交（Read Uncommitted） <a class="header-anchor" href="#读未提交-read-uncommitted" aria-label="Permalink to &quot;读未提交（Read Uncommitted）&quot;">​</a></h3><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202302271437588.png" alt="image-20230227143708414" style="zoom:80%;"><p>先把事务隔离级别设置为read uncommitted，开启事务A，查询id=1的数据</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">session</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">transaction</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">isolation</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">level</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">uncommitted</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">begin</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> account </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> id </span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p>结果如下：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202207201408046.png" alt="image-20220720140823967" style="zoom:67%;"><p>这时候，另开一个窗口打开mysql，也把当前事务隔离级别设置为read uncommitted，开启事务B，执行更新操作</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">session</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">transaction</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">isolation</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">level</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">uncommitted</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">begin</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> account </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> balance</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">balance</span><span style="color:#89DDFF;">+</span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> id </span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><blockquote><p>可以发现，在<strong>读未提交（Read Uncommitted）</strong> 隔离级别下，一个事务会读到其他事务未提交的数据的，即存在<strong>脏读</strong>问题。事务B都还没commit到数据库呢，事务A就读到了，感觉都乱套了。。。实际上，读未提交是隔离级别最低的一种。</p></blockquote><h3 id="读已提交-read-committed" tabindex="-1">读已提交（Read committed） <a class="header-anchor" href="#读已提交-read-committed" aria-label="Permalink to &quot;读已提交（Read committed）&quot;">​</a></h3><p>为了避免脏读，数据库有了比<strong>读未提交</strong>更高的隔离级别，即<strong>读已提交</strong>。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202207201411173.png" alt="image-20220720141131074" style="zoom:67%;"><p>把当前事务隔离级别设置为读已提交（READ COMMITTED），开启事务A，查询account中id=1的数据</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">session</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">transaction</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">isolation</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">level</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">committed</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">begin</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> account </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> id </span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p>另开一个窗口打开mysql，也把事务隔离级别设置为read committed，开启事务B，执行以下操作</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">session</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">transaction</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">isolation</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">level</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">committed</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">begin</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> account </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> balance</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">balance</span><span style="color:#89DDFF;">+</span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> id </span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p>接着回事务A的窗口，再查account数据，发现数据没变：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202207201414572.png" alt="image-20220720141452499" style="zoom:80%;"><p>我们再去到事务B的窗口执行commit操作：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">commit</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p>最后回到事务A窗口查询，发现数据变了：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202207201415789.png" alt="image-20220720141515717" style="zoom:67%;"><blockquote><p>由此可以得出结论，隔离级别设置为<strong>已提交读（READ COMMITTED）</strong> 时，已经不会出现脏读问题了，当前事务只能读取到其他事务提交的数据。但是，你站在事务A的角度想想，存在其他问题吗？</p></blockquote><p><strong>读已提交的隔离级别会有什么问题呢？</strong></p><blockquote><p><strong>在同一个事务A里，相同的查询sql，读取同一条记录（id=1），读到的结果是不一样的</strong>，即<strong>不可重复读</strong>。所以，隔离级别设置为read committed的时候，还会存在<strong>不可重复读</strong>的并发问题。</p></blockquote><h3 id="可重复读-repeatable-read" tabindex="-1">可重复读（Repeatable Read） <a class="header-anchor" href="#可重复读-repeatable-read" aria-label="Permalink to &quot;可重复读（Repeatable Read）&quot;">​</a></h3><blockquote><p>如果你的老板要求，在同个事务中，查询结果必须是一致的，即老板要求你解决不可重复的并发问题，怎么办呢？老板，臣妾办不到？来实践一下<strong>可重复读（Repeatable Read）</strong> 这个隔离级别吧~</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202207201413824.png" alt="image-20220720141351724" style="zoom:67%;"><blockquote><p>哈哈，步骤1、2、6的查询结果都是一样的，即<strong>repeatable read解决了不可重复读问题</strong>，是不是心里美滋滋的呢，终于解决老板的难题了~</p></blockquote><p><strong>RR级别是否解决了幻读问题呢？</strong></p><blockquote><p>再来看看网上的一个热点问题，有关于RR级别下，是否解决了幻读问题？我们来实践一下：</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202207201414538.png" alt="image-20220720141406439" style="zoom:80%;"><blockquote><p>由图可得，步骤2和步骤6查询结果集没有变化，看起来RR级别是已经解决幻读问题了~ 但是呢，<strong>RR级别还是存在这种现象</strong>：</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202207201414793.png" alt="image-20220720141420689" style="zoom:80%;"><p>其实，上图如果事务A中，没有 <code>update account set balance=200 where id = 5;</code>这步操作， <code>select * from account where id &gt; 2</code>查询到的结果集确实是不变，这种情况没有<strong>幻读</strong>问题。但是，有了update这个骚操作，同一个事务，相同的sql，查出的结果集不同，这个是符合了<strong>幻读</strong>的定义~</p><p>这个问题，亲爱的朋友，你觉得它算幻读问题吗？</p><h3 id="串行化-serializable" tabindex="-1">串行化（Serializable） <a class="header-anchor" href="#串行化-serializable" aria-label="Permalink to &quot;串行化（Serializable）&quot;">​</a></h3><blockquote><p>前面三种数据库隔离级别，都有一定的并发问题，现在放大招吧，实践SERIALIZABLE隔离级别。</p></blockquote><blockquote><p>把事务隔离级别设置为Serializable，开启事务A，查询account表数据</p></blockquote><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">session</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">transaction</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">isolation</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">level</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">serializable</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> @@tx_isolation;</span></span>
<span class="line"><span style="color:#F78C6C;">begin</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> account;</span></span></code></pre></div><p>另开一个窗口打开mysql，也把事务隔离级别设置为Serializable，开启事务B，执行插入一条数据：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">session</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">transaction</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">isolation</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">level</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">serializable</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> @@tx_isolation;</span></span>
<span class="line"><span style="color:#F78C6C;">begin</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">insert into</span><span style="color:#A6ACCD;"> account(id,</span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">,balance) </span><span style="color:#F78C6C;">value</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">6</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Li</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">100</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><p>执行结果如下：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202207201412484.png" alt="image-20220720141246376" style="zoom:67%;"><blockquote><p>由图可得，当数据库隔离级别设置为serializable的时候，事务B对表的写操作，在等事务A的读操作。其实，这是隔离级别中最严格的，读写都不允许并发。它保证了最好的安全性，性能却是个问题~</p></blockquote><h2 id="实战演练⭐-1" tabindex="-1">实战演练⭐ <a class="header-anchor" href="#实战演练⭐-1" aria-label="Permalink to &quot;实战演练⭐&quot;">​</a></h2><blockquote><p>注意：一次执行中rollback和commit只执行一个</p></blockquote><h3 id="数据准备-1" tabindex="-1">数据准备 <a class="header-anchor" href="#数据准备-1" aria-label="Permalink to &quot;数据准备&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">drop</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">exists</span><span style="color:#A6ACCD;"> account;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 数据准备</span></span>
<span class="line"><span style="color:#F78C6C;">create</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">table</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">account</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;">    id </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> auto_increment </span><span style="color:#C792EA;">primary key</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">主键ID</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">varchar</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">姓名</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">money</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">int</span><span style="color:#A6ACCD;"> comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">余额</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;">) comment </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">账户表</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">insert into</span><span style="color:#A6ACCD;"> account(id, </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">, </span><span style="color:#C792EA;">money</span><span style="color:#A6ACCD;">) </span><span style="color:#F78C6C;">VALUES</span><span style="color:#A6ACCD;"> (</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张三</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2000</span><span style="color:#A6ACCD;">),(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">李四</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2000</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="未控制事务" tabindex="-1">未控制事务 <a class="header-anchor" href="#未控制事务" aria-label="Permalink to &quot;未控制事务&quot;">​</a></h3><h4 id="测试正常情况" tabindex="-1">测试正常情况 <a class="header-anchor" href="#测试正常情况" aria-label="Permalink to &quot;测试正常情况&quot;">​</a></h4><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 1. 查询张三余额 </span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> account </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张三</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">; </span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 2. 张三的余额减少1000 </span></span>
<span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> account </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">money</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">money</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1000</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张三</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">; </span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 3. 李四的余额增加1000 </span></span>
<span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> account </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">money</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">money</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1000</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">李四</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 4. 查询账户情况</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> account;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209201003186.png" alt="image-20220920100307996" style="zoom:80%;"><h4 id="测试异常情况" tabindex="-1">测试异常情况 <a class="header-anchor" href="#测试异常情况" aria-label="Permalink to &quot;测试异常情况&quot;">​</a></h4><p>这样直接全部执行就行</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 还原数据</span></span>
<span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> account </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">money</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2000</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张三</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">or</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">李四</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 1. 查询张三余额 </span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> account </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张三</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">; </span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 2. 张三的余额减少1000 </span></span>
<span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> account </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">money</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">money</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1000</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张三</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">; </span></span>
<span class="line"><span style="color:#A6ACCD;">出错了.... </span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 3. 李四的余额增加1000 </span></span>
<span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> account </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">money</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">money</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1000</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">李四</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><blockquote><p>我们把数据都恢复到2000， 然后再次一次性执行上述的SQL语句(出错了.... 这句话不符合SQL语 法,执行就会报错)，检查最终的数据情况, 发现数据在操作前后不一致了。</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209201005010.png" alt="image-20220920100512819" style="zoom:80%;"><h3 id="使用事务控制⭐" tabindex="-1">使用事务控制⭐ <a class="header-anchor" href="#使用事务控制⭐" aria-label="Permalink to &quot;使用事务控制⭐&quot;">​</a></h3><p>即使中间发生错误，也能自动回滚，不会发生错误</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 恢复数据</span></span>
<span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> account </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">money</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2000</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张三</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">or</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">李四</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 转账操作 (张三给李四转账1000)</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 开启事务</span></span>
<span class="line"><span style="color:#F78C6C;">start transaction</span><span style="color:#A6ACCD;"> ;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 1. 查询张三账户余额</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> account </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张三</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 2. 将张三账户余额-1000</span></span>
<span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> account </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">money</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">money</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1000</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">张三</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">程序执行报错 ...</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 3. 将李四账户余额+1000</span></span>
<span class="line"><span style="color:#F78C6C;">update</span><span style="color:#A6ACCD;"> account </span><span style="color:#F78C6C;">set</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">money</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">money</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1000</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">where</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">李四</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p>此时发现报错，再去查看，数据已经修改</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209201010487.png" alt="image-20220920101050294" style="zoom:80%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209201012345.png" alt="image-20220920101254156" style="zoom:80%;"><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 提交事务（此时不能进行提交，要执行回滚）</span></span>
<span class="line"><span style="color:#F78C6C;">commit</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 回滚事务(commit之后就不能rollback了)</span></span>
<span class="line"><span style="color:#F78C6C;">rollback</span><span style="color:#A6ACCD;"> ;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209201014018.png" style="zoom:80%;"></div></div></main><footer class="VPDocFooter" data-v-6b87e69f data-v-37656e44><!--[--><!--]--><!----><nav class="prev-next" data-v-37656e44><div class="pager" data-v-37656e44><a class="pager-link prev" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/Netty.html" data-v-37656e44><span class="desc" data-v-37656e44>Previous page</span><span class="title" data-v-37656e44>Netty</span></a></div><div class="pager" data-v-37656e44><a class="pager-link next" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E8%BF%9B%E9%98%B6.html" data-v-37656e44><span class="desc" data-v-37656e44>Next page</span><span class="title" data-v-37656e44>MySQL进阶</span></a></div></nav></footer><!--[--><!--]--></div></div></div><!--[--><!--]--></div></div><!----><!--[--><!--]--></div></div>
    <script>window.__VP_HASH_MAP__=JSON.parse("{\"2、数据库_mysql_mysql面试_基础.md\":\"40da680a\",\"1、学前端_5、小程序_小程序项目.md\":\"60a1629b\",\"1、学前端_4、node_知识篇.md\":\"a7fb500e\",\"1、学前端_2、js_ts_es6 进阶.md\":\"6d07ba10\",\"1、学前端_3、vue_vue3_vue3进阶.md\":\"7ac622b4\",\"5、运维_jenkins.md\":\"929081f8\",\"1、学前端_2、js_ts_typescript.md\":\"875a4aa4\",\"2、数据库_mysql_mysql核心_设计.md\":\"7faf46d1\",\"2、数据库_mysql_mysql核心_基础.md\":\"d8e97f3e\",\"1、学前端_1、html_css_html基础.md\":\"7584d076\",\"1、学前端_5、专题篇_问题篇.md\":\"e893aaa2\",\"2、数据库_mysql_mysql面试_进阶.md\":\"f934806d\",\"3、springboot_运维_原理.md\":\"f4a39db6\",\"2、数据库_influxdb.md\":\"6e1711e1\",\"3、springboot_新特性.md\":\"cdf3e307\",\"mybatis_mybatisplus_jpa.md\":\"8e41681b\",\"1、学前端_5、小程序_小程序优化.md\":\"a2185198\",\"2、数据库_redis_redis基础.md\":\"856df0e0\",\"linux_实用脚本.md\":\"f2299dd5\",\"4、微服务_必备_分布式基础.md\":\"d49863d5\",\"2、数据库_redis_redis优化.md\":\"e66ae32f\",\"4、微服务_springsecurity_进阶篇.md\":\"235a8e9e\",\"5、运维_chatgpt.md\":\"10db3823\",\"2、数据库_mysql_分库分表.md\":\"e1c8a095\",\"start.md\":\"9bc1ff8d\",\"5、运维_github.md\":\"2ec6c735\",\"java学前端_css.md\":\"f11b47f0\",\"1、学前端_5、专题篇_知识篇.md\":\"a463ed8d\",\"linux_软件部署.md\":\"d6722925\",\"2、数据库_neo4j.md\":\"97ad22ac\",\"team.md\":\"ce467a6a\",\"nginx_实战篇.md\":\"7785486e\",\"index.md\":\"8c3ec167\",\"计算机基础_计算机网络_网络基础.md\":\"7a54a85d\",\"1、学前端_4、node_进阶篇.md\":\"60f6db69\",\"java_java集合.md\":\"a049b313\",\"1、学前端_3、vue_vue3_vue3高级.md\":\"614d1516\",\"1、学前端_5、小程序_微信小程序.md\":\"9a4be771\",\"5、运维_netty.md\":\"12ca0278\",\"2、数据库_mysql_mysql核心_运维.md\":\"83f97c16\",\"idea_vs code.md\":\"afdcb593\",\"java学前端_vue3_组件.md\":\"1086884e\",\"idea_chrome.md\":\"4a32afbc\",\"云原生_k8s.md\":\"db58e65a\",\"2、数据库_mysql_mysql核心_进阶.md\":\"61d16dff\",\"ssm_springbatch.md\":\"f799ab4a\",\"三高_分布式.md\":\"db1b8a1b\",\"2、数据库_elasticsearch_1、es基础.md\":\"04d17448\",\"linux_linux基础.md\":\"4b0bf394\",\"idea_idea插件.md\":\"fa86e45a\",\"可视化 _ 监控_可视化大屏.md\":\"004553bd\",\"2、数据库_mongodb_整合.md\":\"3c47d7f4\",\"4、微服务_springsecurity_基础篇.md\":\"534a3401\",\"4、微服务_进阶.md\":\"69095c58\",\"计算机基础_计算机基础_操作系统.md\":\"0f75d113\",\"可视化 _ 监控_zabbix.md\":\"71f2270e\",\"nginx_基础篇.md\":\"c7d8bb50\",\"1、学前端_4、node_项目实战.md\":\"bc5065b8\",\"2、数据库_redis_redis原理.md\":\"5cedf685\",\"可视化 _ 监控_监控基础.md\":\"ac56ce4d\",\"三高_高并发.md\":\"ea9ffc99\",\"2、数据库_redis_redis高级.md\":\"1d5872f6\",\"1、学前端_4、node_基础篇.md\":\"581cc13a\",\"2、数据库_mongodb_基础.md\":\"fb7a0a29\",\"idea_idea基础.md\":\"6f2f9638\",\"4、微服务_必备_sentinel.md\":\"2edfbf6c\",\"2、数据库_elasticsearch_3、es高级.md\":\"ef146606\",\"1、学前端_3、vue_vue3_vue3新语法.md\":\"8afd5409\",\"消息中间件_canal.md\":\"3949163c\",\"ssm_maven.md\":\"2c5e12ed\",\"4、微服务_springsecurity_高级篇.md\":\"882d3ff3\",\"linux_linux进阶.md\":\"188ef7b4\",\"计算机基础_设计模式_uml.md\":\"634ba256\",\"计算机基础_算法_leetcode.md\":\"77162fb9\",\"项目实战_小兔鲜_进阶篇1.md\":\"17c52c81\",\"1、学前端_2、js_ts_es6 基础.md\":\"fda3f18b\",\"项目实战_小兔鲜_进阶篇2.md\":\"a0f23006\",\"软件测试_测试基础.md\":\"8c1060cd\",\"2、数据库_redis_本地缓存.md\":\"00617fe6\",\"nginx_面试篇.md\":\"e3fb373a\",\"mybatis_mybatisplus_mybatis.md\":\"9239e0ad\",\"linux_shell.md\":\"ae53d83b\",\"2、数据库_mysql_mysql核心_优化.md\":\"36230425\",\"项目实战_项目推荐.md\":\"f9d97630\",\"mybatis_mybatisplus_mybatisplus.md\":\"0030fd35\",\"项目实战_百度地图_进阶篇.md\":\"c8b93267\",\"三高_高可用.md\":\"323840c5\",\"java_java新特性.md\":\"22abf56d\",\"软件测试_压力测试.md\":\"9ab44440\",\"java学前端_html_js.md\":\"e0fcd240\",\"2、数据库_redis_redis实战.md\":\"d6daeeab\",\"nginx_进阶篇.md\":\"e6b63195\",\"三高_秒杀.md\":\"3878bb64\",\"5、运维_git.md\":\"0264925c\",\"java_java进阶.md\":\"e79cb5b4\",\"并发 _ 多线程_基础篇.md\":\"7adbfac5\",\"项目实战_百度地图_基础篇.md\":\"8afa5954\",\"java学前端_react.md\":\"3ec827dd\",\"1、学前端_1、html_css_css基础.md\":\"01b56712\",\"项目实战_小兔鲜_基础篇.md\":\"646f5df5\",\"1、学前端_2、js_ts_js 基础.md\":\"cb13e36f\",\"可视化 _ 监控_监控进阶.md\":\"0cdbc292\",\"计算机基础_设计模式_基础篇.md\":\"51617287\",\"计算机基础_数据结构_基础篇.md\":\"b2bfd8d4\",\"项目实战_苍穹外卖_进阶篇.md\":\"48415e41\",\"ssm_spring.md\":\"ab514659\",\"消息中间件_rabbitmq.md\":\"45b1eb28\",\"1、学前端_1、html_css_网页进阶.md\":\"db998248\",\"消息中间件_kafka.md\":\"b747dabf\",\"云原生_docker.md\":\"983c7ba7\",\"4、微服务_必备_分布式锁.md\":\"5af1cf8d\",\"消息中间件_rocketmq.md\":\"d441da85\",\"项目实战_黑马头条_基础篇.md\":\"b05af3a6\",\"ssm_springmvc.md\":\"81b9714f\",\"项目实战_支付.md\":\"1d7407dd\",\"项目实战_黑马头条_进阶篇2.md\":\"bff0015b\",\"项目实战_黑马头条_进阶篇.md\":\"19f18388\",\"java学前端_vue2_组件.md\":\"58c6b1df\",\"3、springboot_基础篇.md\":\"529c66f4\",\"3、springboot_应用篇.md\":\"8b92aa61\",\"项目实战_黑马头条_高级篇.md\":\"227c08c1\",\"1、学前端_5、小程序_uniapp.md\":\"71a282b4\",\"项目实战_云尚办公_基础篇.md\":\"1fe188ba\",\"并发 _ 多线程_并发完善.md\":\"26619c46\",\"1、学前端_2、js_ts_js 进阶.md\":\"657dfb8f\",\"java_java高级.md\":\"23782d1a\",\"java_java基础.md\":\"86d67c77\"}");window.__VP_SITE_DATA__=JSON.parse("{\"lang\":\"en-US\",\"dir\":\"ltr\",\"title\":\"VitePress\",\"description\":\"A VitePress site\",\"base\":\"/notebook/\",\"head\":[],\"appearance\":true,\"themeConfig\":{\"algolia\":{\"appId\":\"DW7O63I9IR\",\"apiKey\":\"f8ed758cdb288a8b06542bc35923c1a1\",\"indexName\":\"notebook\"},\"sidebar\":[{\"text\":\"Java\",\"collapsed\":true,\"items\":[{\"text\":\"Java基础\",\"link\":\"/Java/Java基础\"},{\"text\":\"Java新特性\",\"link\":\"/Java/Java新特性\"},{\"text\":\"Java进阶\",\"link\":\"/Java/Java进阶\"},{\"text\":\"Java集合\",\"link\":\"/Java/Java集合\"},{\"text\":\"Java高级\",\"link\":\"/Java/Java高级\"}]},{\"text\":\"Linux\",\"collapsed\":true,\"items\":[{\"text\":\"Linux基础\",\"link\":\"/Linux/Linux基础\"},{\"text\":\"Linux新特性\",\"link\":\"/Linux/Linux进阶\"},{\"text\":\"Shell脚本\",\"link\":\"/Linux/Shell\"},{\"text\":\"实用脚本\",\"link\":\"/Linux/实用脚本\"},{\"text\":\"软件部署\",\"link\":\"/Linux/软件部署\"}]},{\"text\":\"Nginx\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/Nginx/基础篇\"},{\"text\":\"进阶篇\",\"link\":\"/Nginx/进阶篇\"},{\"text\":\"实战篇\",\"link\":\"/Nginx/实战篇\"},{\"text\":\"面试篇\",\"link\":\"/Nginx/面试篇\"}]},{\"text\":\"SSM\",\"collapsed\":true,\"items\":[{\"text\":\"Maven\",\"link\":\"/SSM/Maven\"},{\"text\":\"Spring\",\"link\":\"/SSM/Spring\"},{\"text\":\"SpringMVC\",\"link\":\"/SSM/SpringMVC\"},{\"text\":\"SpringBatch\",\"link\":\"/SSM/SpringBatch\"}]},{\"text\":\"SpringBoot\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/3、SpringBoot/基础篇\"},{\"text\":\"应用篇\",\"link\":\"/3、SpringBoot/应用篇\"},{\"text\":\"新特性\",\"link\":\"/3、SpringBoot/新特性\"},{\"text\":\"运维&原理\",\"link\":\"/3、SpringBoot/运维&原理\"}]},{\"text\":\"SpringCloud\",\"collapsed\":true,\"items\":[{\"text\":\"SpringCloud\",\"link\":\"/4、微服务/进阶\"},{\"text\":\"Sentinel\",\"link\":\"/4、微服务/必备/Sentinel\"}]},{\"text\":\"SpringSecurity\",\"collapsed\":true,\"items\":[{\"text\":\"SpringSecurity基础篇\",\"link\":\"/4、微服务/SpringSecurity/基础篇\"},{\"text\":\"SpringSecurity进阶篇\",\"link\":\"/4、微服务/SpringSecurity/进阶篇\"},{\"text\":\"SpringSecurity高级篇\",\"link\":\"/4、微服务/SpringSecurity/高级篇\"}]},{\"text\":\"Mybatis & MybatisPlus\",\"collapsed\":true,\"items\":[{\"text\":\"Mybatis\",\"link\":\"/Mybatis&MybatisPlus/Mybatis\"},{\"text\":\"MybatisPlus\",\"link\":\"/Mybatis&MybatisPlus/MybatisPlus\"},{\"text\":\"JPA\",\"link\":\"/Mybatis&MybatisPlus/JPA\"}]},{\"text\":\"Git & ChatGPT\",\"collapsed\":true,\"items\":[{\"text\":\"Git\",\"link\":\"/5、运维/Git\"},{\"text\":\"Github\",\"link\":\"/5、运维/Github\"},{\"text\":\"ChatGPT\",\"link\":\"/5、运维/ChatGPT\"},{\"text\":\"Jenkins\",\"link\":\"/5、运维/Jenkins\"},{\"text\":\"Netty\",\"link\":\"/5、运维/Netty\"}]},{\"text\":\"数据库\",\"collapsed\":true,\"items\":[{\"text\":\"MySQL\",\"collapsed\":true,\"items\":[{\"text\":\"MySQL基础\",\"link\":\"/2、数据库/MySQL/MySQL核心/基础\"},{\"text\":\"MySQL进阶\",\"link\":\"/2、数据库/MySQL/MySQL核心/进阶\"},{\"text\":\"MySQL优化\",\"link\":\"/2、数据库/MySQL/MySQL核心/优化\"},{\"text\":\"MySQL设计\",\"link\":\"/2、数据库/MySQL/MySQL核心/设计\"},{\"text\":\"MySQL运维\",\"link\":\"/2、数据库/MySQL/MySQL核心/运维\"},{\"text\":\"分库分表\",\"link\":\"/2、数据库/MySQL/分库分表\"}]},{\"text\":\"Redis\",\"collapsed\":true,\"items\":[{\"text\":\"Redis基础\",\"link\":\"/2、数据库/Redis/Redis基础\"},{\"text\":\"Redis优化\",\"link\":\"/2、数据库/Redis/Redis优化\"},{\"text\":\"Redis原理\",\"link\":\"/2、数据库/Redis/Redis原理\"},{\"text\":\"Redis高级\",\"link\":\"/2、数据库/Redis/Redis高级\"},{\"text\":\"Redis实战\",\"link\":\"/2、数据库/Redis/Redis实战\"},{\"text\":\"本地缓存\",\"link\":\"/2、数据库/Redis/本地缓存\"}]},{\"text\":\"MongoDB\",\"collapsed\":true,\"items\":[{\"text\":\"MongoDB基础\",\"link\":\"/2、数据库/MongoDB/基础\"},{\"text\":\"MongoDB进阶\",\"link\":\"/2、数据库/MongoDB/整合\"}]},{\"text\":\"ElasticSearch\",\"collapsed\":true,\"items\":[{\"text\":\"ES基础\",\"link\":\"/2、数据库/ElasticSearch/1、ES基础\"},{\"text\":\"ES高级\",\"link\":\"/2、数据库/ElasticSearch/3、ES高级\"}]},{\"text\":\"InfluxDB\",\"link\":\"/2、数据库/influxdb\"},{\"text\":\"Neo4j\",\"link\":\"/2、数据库/Neo4j\"}]},{\"text\":\"高并发 & 秒杀 & 分布式\",\"collapsed\":true,\"items\":[{\"text\":\"分布式理论\",\"link\":\"/三高/分布式\"},{\"text\":\"分布式锁\",\"link\":\"/4、微服务/必备/分布式锁\"},{\"text\":\"秒杀\",\"link\":\"/三高/秒杀\"},{\"text\":\"高可用\",\"link\":\"/三高/高可用\"},{\"text\":\"高并发\",\"link\":\"/三高/高并发\"}]},{\"text\":\"云原生\",\"collapsed\":true,\"items\":[{\"text\":\"Docker\",\"link\":\"/云原生/Docker\"},{\"text\":\"K8S\",\"link\":\"/云原生/K8S\"}]},{\"text\":\"可视化 & 监控\",\"collapsed\":true,\"items\":[{\"text\":\"监控基础\",\"link\":\"/可视化 & 监控/监控基础\"},{\"text\":\"监控进阶\",\"link\":\"/可视化 & 监控/监控进阶\"},{\"text\":\"可视化大屏\",\"link\":\"/可视化 & 监控/可视化大屏\"},{\"text\":\"Zabbix\",\"link\":\"/可视化 & 监控/Zabbix\"}]},{\"text\":\"学前端\",\"collapsed\":true,\"items\":[{\"text\":\"HTML+CSS\",\"collapsed\":true,\"items\":[{\"text\":\"HTML基础\",\"link\":\"/1、学前端/1、HTML+CSS/HTML基础\"},{\"text\":\"CSS基础\",\"link\":\"/1、学前端/1、HTML+CSS/CSS基础\"},{\"text\":\"网页进阶\",\"link\":\"/1、学前端/1、HTML+CSS/网页进阶\"}]},{\"text\":\"JS+TS\",\"collapsed\":true,\"items\":[{\"text\":\"JS基础\",\"link\":\"/1、学前端/2、JS+TS/JS 基础\"},{\"text\":\"JS进阶\",\"link\":\"/1、学前端/2、JS+TS/JS 进阶\"},{\"text\":\"ES6基础\",\"link\":\"/1、学前端/2、JS+TS/ES6 基础\"},{\"text\":\"ES6进阶\",\"link\":\"/1、学前端/2、JS+TS/ES6 进阶\"},{\"text\":\"TS基础\",\"link\":\"/1、学前端/2、JS+TS/TypeScript\"}]},{\"text\":\"NodeJS\",\"collapsed\":true,\"items\":[{\"text\":\"Node基础\",\"link\":\"/1、学前端/4、Node/基础篇\"},{\"text\":\"Node进阶\",\"link\":\"/1、学前端/4、Node/进阶篇\"},{\"text\":\"项目实战\",\"link\":\"/1、学前端/4、Node/项目实战\"}]},{\"text\":\"Vue\",\"collapsed\":true,\"items\":[{\"text\":\"Vue3进阶\",\"link\":\"/1、学前端/3、Vue/Vue3/Vue3进阶\"},{\"text\":\"Vue3高级\",\"link\":\"/1、学前端/3、Vue/Vue3/Vue3高级\"},{\"text\":\"Vue3新语法\",\"link\":\"/1、学前端/3、Vue/Vue3/Vue3新语法\"},{\"text\":\"项目实战\",\"link\":\"/1、学前端/3、Vue/Vue2/Vue2项目\"}]},{\"text\":\"小程序\",\"collapsed\":true,\"items\":[{\"text\":\"小程序基础\",\"link\":\"/1、学前端/5、小程序/微信小程序\"},{\"text\":\"小程序优化\",\"link\":\"/1、学前端/5、小程序/小程序优化\"},{\"text\":\"uniapp\",\"link\":\"/1、学前端/5、小程序/uniapp\"},{\"text\":\"项目实战\",\"link\":\"/1、学前端/5、小程序/小程序项目\"}]}]},{\"text\":\"计算机基础\",\"collapsed\":true,\"items\":[{\"text\":\"数据结构\",\"link\":\"/计算机基础/数据结构/基础篇\"},{\"text\":\"操作系统\",\"link\":\"/计算机基础/计算机基础/操作系统\"},{\"text\":\"设计模式\",\"link\":\"/计算机基础/设计模式/基础篇\"},{\"text\":\"计算机网络\",\"link\":\"/计算机基础/计算机网络/网络基础\"},{\"text\":\"UML\",\"link\":\"/计算机基础/设计模式/UML\"},{\"text\":\"LeetCode\",\"link\":\"/计算机基础/算法/LeetCode\"}]},{\"text\":\"项目实战\",\"collapsed\":true,\"items\":[{\"text\":\"云尚办公\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/项目实战/云尚办公/基础篇\"}]},{\"text\":\"小兔鲜\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/项目实战/小兔鲜/基础篇\"},{\"text\":\"进阶篇1\",\"link\":\"/项目实战/小兔鲜/进阶篇1\"},{\"text\":\"进阶篇2\",\"link\":\"/项目实战/小兔鲜/进阶篇2\"}]},{\"text\":\"地图\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/项目实战/百度地图/基础篇\"},{\"text\":\"进阶篇\",\"link\":\"/项目实战/百度地图/进阶篇\"}]},{\"text\":\"苍穹外卖\",\"collapsed\":true,\"items\":[{\"text\":\"进阶篇\",\"link\":\"/项目实战/苍穹外卖/进阶篇\"}]},{\"text\":\"黑马头条\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/项目实战/黑马头条/基础篇\"},{\"text\":\"进阶篇\",\"link\":\"/项目实战/黑马头条/进阶篇\"},{\"text\":\"进阶篇2\",\"link\":\"/项目实战/黑马头条/进阶篇2\"},{\"text\":\"高级篇\",\"link\":\"/项目实战/黑马头条/高级篇\"}]},{\"text\":\"支付\",\"link\":\"/项目实战/支付\"},{\"text\":\"项目推荐\",\"link\":\"/项目实战/项目推荐\"}]},{\"text\":\"团队成员\",\"link\":\"/team\"}],\"siteTitle\":\"任硕的文档\",\"logo\":\"/Vue.png\",\"nav\":[{\"text\":\"Java学前端\",\"items\":[{\"items\":[{\"text\":\"HTML+JS\",\"link\":\"/Java学前端/HTML+JS\"},{\"text\":\"CSS\",\"link\":\"/Java学前端/CSS\"},{\"text\":\"Vue2+组件\",\"link\":\"/Java学前端/Vue2+组件\"},{\"text\":\"Vue3+组件\",\"link\":\"/Java学前端/Vue3+组件\"},{\"text\":\"React\",\"link\":\"/Java学前端/React\"}]}],\"activeMatch\":\"/Java/\"},{\"text\":\"软件测试\",\"items\":[{\"items\":[{\"text\":\"测试基础\",\"link\":\"/软件测试/测试基础\"},{\"text\":\"压力测试\",\"link\":\"/软件测试/压力测试\"}]}]},{\"text\":\"多线程\",\"items\":[{\"items\":[{\"text\":\"基础篇\",\"link\":\"/并发 & 多线程/基础篇\"},{\"text\":\"进阶篇\",\"link\":\"/并发 & 多线程/并发完善\"}]}]},{\"text\":\"开发工具\",\"items\":[{\"items\":[{\"text\":\"Chrome\",\"link\":\"/IDEA/Chrome\"},{\"text\":\"IDEA基础\",\"link\":\"/IDEA/IDEA基础\"},{\"text\":\"IDEA插件\",\"link\":\"/IDEA/IDEA插件\"},{\"text\":\"VS Code\",\"link\":\"/IDEA/VS Code\"}]}]},{\"text\":\"消息中间件\",\"items\":[{\"items\":[{\"text\":\"RabbitMQ\",\"link\":\"/消息中间件/RabbitMQ\"},{\"text\":\"RocketMQ\",\"link\":\"/消息中间件/RocketMQ\"},{\"text\":\"Kafka\",\"link\":\"/消息中间件/Kafka\"},{\"text\":\"Canal\",\"link\":\"/消息中间件/Canal\"}]}]}],\"socialLinks\":[{\"icon\":\"github\",\"link\":\"https://github.com/renshuo123/renshuo123.github.io\"},{\"icon\":\"twitter\",\"link\":\"#\"},{\"icon\":{\"svg\":\"<svg t=\\\"1676028692954\\\" class=\\\"icon\\\" ...</path></svg>\"},\"link\":\"https://github.com/\"}]},\"locales\":{},\"scrollOffset\":90,\"cleanUrls\":false}");</script>
    
  </body>
</html>